bumptech/glide · error · IllegalArgumentException
You cannot start a load on a fragment before it is attached
Error message
You cannot start a load on a fragment before it is attached
What it means
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.
Source
Thrown at library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java:250
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static void assertNotDestroyed(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
throw new IllegalArgumentException("You cannot start a load for a destroyed activity");
}
}
/**
* @deprecated This is equivalent to calling {@link #get(Context)} with the application context.
* Use androidx fragments instead: {@link Fragment}.
*/
@Deprecated
@NonNull
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public RequestManager get(@NonNull android.app.Fragment fragment) {
if (fragment.getActivity() == null) {
throw new IllegalArgumentException(
"You cannot start a load on a fragment before it is attached");
}
return get(fragment.getActivity().getApplicationContext());
}
private static boolean isActivityVisible(Context context) {
// This is a poor heuristic, but it's about all we have. We'd rather err on the side of visible
// and start requests than on the side of invisible and ignore valid requests.
Activity activity = findActivity(context);
return activity == null || !activity.isFinishing();
}
/**
* @deprecated This method is no longer called by Glide or provides any functionality and it will
* be removed in the future. Retained for now to preserve backwards compatibility.
*/
@Deprecated
@SuppressWarnings("PMD.CollapsibleIfStatements")View on GitHub (pinned to eb14a895d8)
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
Example fix
// before
public View onCreateView(...) {
Glide.with(this).load(url).into(imageView); // 'this' is android.app.Fragment, not attached yet
}
// after
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (getActivity() != null) {
Glide.with(getActivity().getApplicationContext()).load(url).into(imageView);
}
} Defensive patterns
Strategy: validation
Validate before calling
if (fragment.activity == null) {
// defer the load to onAttach, or fall back to application context
return
}
Glide.with(fragment).load(url).into(imageView) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to find GlideModule implementation
- Cannot restart a running request
- You can't start or clear loads in RequestListener or Target
- Already released
- sizeMultiplier must be between 0 and 1
AI-assisted analysis of bumptech/glide@eb14a895d8 (2026-08-14).
Data as JSON: /api/errors/50a86667f3f319d6.
Report an issue: GitHub.