My glide version:
compile 'com.github.bumptech.glide:glide:3.7.0'
java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
at com.bumptech.glide.Glide.with(Glide.java:653)
Yes? You cannot start a load for a destroyed activity. There's no better way of saying it. If the activity is destroyed it won't ever be visible again, the user can't see your image, so there's no point in even trying to start to download it. See #803 for more discussion.
Getting Same Problem.
.... crash , why not log ?
Same problem. Why is it a crash and not just log message?
I got the same problem, how to stop this crash? i am using gradle 'com.github.bumptech.glide:glide:3.7.0'
Hi @all. the problem occurs most when your activity is recreated and the context that's in Glide is an old one. for instance when you have a CustomAdapter(ArrayList list, Context context), and in MainActivity or Fragment recreation, you're not passing the new context to the adapter. Glide then tells you that the context i'm using does no more exist. I hope you understood that. whoever needs a debug can invite me to contribute in his project.
What is a workaround for this issue?
I fixed this by using getApplicationContext()
It's dangerous to load images with App context, they won't be recycled when
the activity dies
Le 6 avr. 2017 1:06 PM, "Darran Kelinske" notifications@github.com a
écrit :
I fixed this by using getApplicationContext()
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/bumptech/glide/issues/1484#issuecomment-292153414,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALgcboB-HlC_FBnE4WWtId558DxkzoCBks5rtNUkgaJpZM4KGRnN
.
Can you please provide an effective alternative then?
Guys, read the duplicate issue; particularly: Dec 10-14.
That looks like a better workaround than app-context, especially in @neonwarge04's case (why did you remove your comment? [it was about cancelling retrofit calls, but still getting the crash])
Will check it out. Thank you! :)
Got the same issue today when using version compiled into FirebaseUI v.2.0.1. I was scrolling my recyclerview and error occurs.
It can be fix by using:
if (!YourActivity.this.isFinishing()) {
Glide.with(...
}
I fixed this using getApplicationContext() while initializing the glide as Glide.with(getApplicationContext()) and pause all request by overriding onDestroy() as
@Override
protected void onDestroy() {
super.onDestroy();
Glide.with(getApplicationContext()).pauseRequests();
}
taranjeetspra, it will blocking pause all Glide requests - if you use a service that fires notifications and it uses Glide for the avatar - it will block the notification.
@targetapi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static boolean isActivityDestroyed(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
&& activity != null && activity.isDestroyed()) {
return true;
}
return false;
}
when you load image in Activity, please check like this. If the activity isDestroyed, stop load image by Glide, else load image by Glide
@meikaiss
I tried doing what you did but I am still seeing the issue
I am using following util function before loading any image
final Context context = getContext();
if (isValidContextForGlide(context) {
// Load image via Glide lib using context
}
public static boolean isValidContextForGlide(final Context context) {
if (context == null) {
return false;
}
if (context instanceof Activity) {
final Activity activity = (Activity) context;
if (activity.isDestroyed() || activity.isFinishing()) {
return false;
}
}
return true;
}
Code Ref:
@farhan yup, that's what i had to end up doing. should have updated it here.
Use Glide.with(getApplicationContext() instead of Glide.with(YourActivity.this)
OR
if you are using an adapter or class that does not extend Context or its sub-classes (Activity, etc)
use Glide.with(mContext.getApplicationContext() where mContext is the context passed to the class.
@Emeritus-DarranKelinske @mohammadreza @taranjeetsapra @meikaiss @sreejithraman @farhan @pulkitrathi17 please don't use Application context, see https://stackoverflow.com/a/32887693/253468 and conversation around https://github.com/bumptech/glide/issues/803#issuecomment-163880203.
Pass the RequestManager into the adapter, or save it in a field with the closest possible context (potentially a fragment, or even a view). This way there's no need for checking, no global loads (easy leaks), testable, etc.
@TWiStErRob that makes sense, but i can't help but feel like that's a bit hacky. Am I wrong to think that? Does Picasso face a similar issue? I don't remember this being an issue in prior Glide versions... what changed?
It's much less hacky than using app-context. It actually uses Glide's feature that it doesn't queue requests on paused request managers. It also encourages proper dependency injection and hence better reuse. (e.g. you could simply do @Inject RequestManager glide; to hide the static call).
Re Picasso and earlier versions, not sure, I started using Glide 3.x and over only. A quick look at http://square.github.io/picasso/2.x/picasso/com/squareup/picasso/Picasso.html#with-android.content.Context- tells me that it returns "The global default Picasso instance." which means Picasso only has the equivalent of Glide.with(applicationContext), so obviously this is a non-issue there.
As mentioned by @farhan , I am using
Context glideContext = isValidContextForGlide(getContext())? getContext() : mActivity.getApplicationContext();
and I am accessing mActivity in Fragment.Hence mActivity.getApplicationContext();
I solve it by
Implement onDestroy LifecycleListener in RequestManager
private val requestManager by lazy { Glide.with(this) }
override fun onDestroy() {
super.onDestroy()
requestManager.onDestroy()
}
use -> requestManager.load().into()
i resolved it using this code android:configChanges="orientation|screenSize" in the AndroidManifest.xml of the destroyed Activity .
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"/>
Please use Glide.with(getApplicationContext()) instead of Glide.with(this)
Please don't use application context, use Glide.with(this) in onCreateto get the best context for resource management and use dependency injection to pass the RequestManager around. You don't even need to check for dying activities and stuff. See https://stackoverflow.com/a/32887693
public static void loadUrl(Context context, String url) {
if (ContextUtil.isDestroyed(context)) {
// 避免在activity销毁后仍加载显示图片
return;
}
Glide.with(context)
.load(url)
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}
public static boolean isDestroyed(Context context) {
if (null == context) {
return true;
}
if (context instanceof Activity) {
Activity activity = (Activity) context;
if (activity.isFinishing()) {
return true;
}
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed();
}
return false;
}
Most helpful comment
I am using following util function before loading any image
Code Ref: