{"record":{"id":"50a86667f3f319d6","repo":"bumptech/glide","slug":"you-cannot-start-a-load-on-a-fragment-before-it-is","errorCode":null,"errorMessage":"You cannot start a load on a fragment before it is attached","messagePattern":"You cannot start a load on a fragment before it is attached","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java","lineNumber":250,"sourceCode":"  }\n\n  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)\n  private static void assertNotDestroyed(@NonNull Activity activity) {\n    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {\n      throw new IllegalArgumentException(\"You cannot start a load for a destroyed activity\");\n    }\n  }\n\n  /**\n   * @deprecated This is equivalent to calling {@link #get(Context)} with the application context.\n   *     Use androidx fragments instead: {@link Fragment}.\n   */\n  @Deprecated\n  @NonNull\n  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)\n  public RequestManager get(@NonNull android.app.Fragment fragment) {\n    if (fragment.getActivity() == null) {\n      throw new IllegalArgumentException(\n          \"You cannot start a load on a fragment before it is attached\");\n    }\n    return get(fragment.getActivity().getApplicationContext());\n  }\n\n  private static boolean isActivityVisible(Context context) {\n    // This is a poor heuristic, but it's about all we have. We'd rather err on the side of visible\n    // and start requests than on the side of invisible and ignore valid requests.\n    Activity activity = findActivity(context);\n    return activity == null || !activity.isFinishing();\n  }\n\n  /**\n   * @deprecated This method is no longer called by Glide or provides any functionality and it will\n   *     be removed in the future. Retained for now to preserve backwards compatibility.\n   */\n  @Deprecated\n  @SuppressWarnings(\"PMD.CollapsibleIfStatements\")","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java#L232-L268","documentation":"Thrown by the deprecated Glide.with(android.app.Fragment) overload when the passed platform Fragment has no attached Activity (fragment.getActivity() == null). Glide derives the Application context from the Activity to scope the RequestManager, so it cannot proceed without one. The method is deprecated precisely because android.app.Fragment is itself deprecated; androidx Fragment is preferred.","triggerScenarios":"Calling Glide.with(androidAppFragment) before the fragment's onAttach(Context/Activity) lifecycle callback has fired, or after onDetach. Common during fragment construction, onCreateView (pre-attach on some versions), or from a retained fragment recreated after a config change before re-attachment.","commonSituations":"Migrating legacy code that still references android.app.Fragment; starting a load in a Fragment field initializer or constructor; calling Glide.with(this) inside a headless/retained fragment before it is added; unit/instrumentation tests that construct a Fragment without an Activity host.","solutions":["Move the Glide.with(fragment) call into or after onAttach(Context) (e.g. onViewCreated or onStart) so getActivity() is non-null","Switch to androidx.fragment.app.Fragment and use the androidx overload Glide.with(androidxFragment) which is not deprecated and has richer lifecycle handling","If you genuinely need a load before attachment, call Glide.with(requireContext().getApplicationContext()) or Glide.with(application) instead","Guard with if (isAdded() && getActivity() != null) before requesting"],"exampleFix":"// before\npublic View onCreateView(...) {\n  Glide.with(this).load(url).into(imageView); // 'this' is android.app.Fragment, not attached yet\n}\n// after\n@Override\npublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {\n  if (getActivity() != null) {\n    Glide.with(getActivity().getApplicationContext()).load(url).into(imageView);\n  }\n}","handlingStrategy":"validation","validationCode":"if (fragment.activity == null) {\n  // defer the load to onAttach, or fall back to application context\n  return\n}\nGlide.with(fragment).load(url).into(imageView)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Start Glide loads in onViewCreated/onStart, never in constructors or field initializers of a Fragment","Prefer androidx.fragment.app.Fragment and the androidx Glide.with(Fragment) overload","Guard with isAdded() / getActivity() != null before any Glide call from a Fragment","Treat android.app.Fragment as fully deprecated; migrate to androidx"],"tags":["android","lifecycle","fragment","deprecated","glide"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}