glide icon indicating copy to clipboard operation
glide copied to clipboard

You cannot start a load for a destroyed activity

Open huzhenjie opened this issue 10 years ago • 32 comments

My glide version:

compile 'com.github.bumptech.glide:glide:3.7.0'

Exception info

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)

huzhenjie avatar Sep 26 '16 08:09 huzhenjie

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.

TWiStErRob avatar Sep 26 '16 09:09 TWiStErRob

Getting Same Problem.

shivangbtech avatar Oct 18 '16 10:10 shivangbtech

.... crash , why not log ?

xiaojidonggong avatar Dec 14 '16 08:12 xiaojidonggong

Same problem. Why is it a crash and not just log message?

tonnyavery avatar Jan 09 '17 14:01 tonnyavery

I got the same problem, how to stop this crash? i am using gradle 'com.github.bumptech.glide:glide:3.7.0'

pankaj89 avatar Jan 10 '17 08:01 pankaj89

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.

steve111MV avatar Jan 12 '17 08:01 steve111MV

What is a workaround for this issue?

Emeritus-DarranKelinske avatar Mar 24 '17 22:03 Emeritus-DarranKelinske

I fixed this by using getApplicationContext()

Emeritus-DarranKelinske avatar Apr 06 '17 12:04 Emeritus-DarranKelinske

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" [email protected] 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 .

steve111MV avatar Apr 06 '17 12:04 steve111MV

Can you please provide an effective alternative then?

Emeritus-DarranKelinske avatar Apr 06 '17 12:04 Emeritus-DarranKelinske

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])

TWiStErRob avatar Apr 06 '17 22:04 TWiStErRob

Will check it out. Thank you! :)

Emeritus-DarranKelinske avatar Apr 07 '17 01:04 Emeritus-DarranKelinske

Got the same issue today when using version compiled into FirebaseUI v.2.0.1. I was scrolling my recyclerview and error occurs.

hkoncept avatar Jul 02 '17 10:07 hkoncept

It can be fix by using:

if (!YourActivity.this.isFinishing()) {
Glide.with(...
}

mrzslr avatar Aug 25 '17 11:08 mrzslr

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(); }

taranjeetsapra avatar Sep 13 '17 17:09 taranjeetsapra

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.

AVancans avatar Sep 15 '17 17:09 AVancans

@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 avatar Nov 20 '17 03:11 meikaiss

@meikaiss I tried doing what you did but I am still seeing the issue

sreejithraman avatar Dec 25 '17 03:12 sreejithraman

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:

  • https://github.com/bumptech/glide/blob/99229725401d5777e059da7b6331134bf73fbcdf/library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java#L321

farhan avatar Feb 14 '18 14:02 farhan

@farhan yup, that's what i had to end up doing. should have updated it here.

sreejithraman avatar Feb 20 '18 20:02 sreejithraman