13

I am using the glide library in my android project to fetch and display images. Earlier I was using version 2.0.5 and facing rendering issue. The problem was that the wrong images were rendering. I have updated library to 3.3 version and it now crashes with the following exception.

14-Sep-2014 08:41:31 PM java.lang.IllegalArgumentException: 
You cannot start a load for a destroyed activity
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:63)
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:29)
    at com.bumptech.glide.Glide.with(Glide.java:537)
    at com.miamiheat.common.MHImageDownloadWrapper.loadImage(MHImageDownloadWrapper.java:12)
    at com.miamiheat.ui.module.MHWallpaperModule.setWallpaperViewData(MHWallpaperModule.java:234)
    at com.miamiheat.ui.module.MHWallpaperModule.taskManagerResponseCallback(MHWallpaperModule.java:257)
    at com.miamiheat.service.taskmanager.MHWallpaperTaskManager.asyncResultCallback(MHWallpaperTaskManager.java:133)
    at com.miamiheat.service.framework.MHAsyncServiceTask.onPostExecute(MHAsyncServiceTask.java:191)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)

Is there anyway to cancel the image download request on activity destroy.

Terrance
  • 11,764
  • 4
  • 54
  • 80
Manish
  • 983
  • 1
  • 9
  • 27
  • I have solved using picasso instead of glide.Check [here](http://stackoverflow.com/questions/39093730/you-cannot-start-a-load-for-a-destroyed-activity-in-relativelayout-image-using-g/39096193#39096193) – Stephen Aug 23 '16 at 08:37

5 Answers5

7

Glide.clear() sometimes doesn't help for me. Pass application context to Glide.with(...) using getApplicationContext() method.

user2028928
  • 91
  • 1
  • 3
4

You can always use Glide.clear() to cancel a load.

However, that exception occurs when you try to start a new load after your activity has been destroyed. Are you starting to load images after fetching some data asynchronously? If so you probably want to cancel your async fetch when your activity is stopped or at least ignore the result if the fetch finishes and your activity is destroyed.

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
  • Hi, I am using Glide.with(context).load(imageUrl).animate(R.anim.fade_in).into(imageView); to load image. It is getting loaded by the list item layout using list adapter. – Manish Sep 16 '14 at 04:58
  • In Glide v4 it is: `Glide.with(fragment).clear(target)` (see the [migration guide](https://bumptech.github.io/glide/doc/migrating.html#cancellation)) – Incinerator Oct 16 '17 at 09:59
3

You must call Glide.with(getActivity()).pauseRequests() or Glide.get(getActivity()).clearMemory() in the method onDestroy() or onDestroyView(), just delete it.

Cthulhu
  • 5,095
  • 7
  • 46
  • 58
AnswerZhao
  • 366
  • 3
  • 13
0

add this before Glade call

if(!this.isDestroyed()){
   Glide.with(activity).load(...)
   ....
}
  • While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/44175333/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL May 25 '17 at 09:15
0

Make sure you clear your view at the beginning of the onBindViewHolder method. That's all.

@Override
public void onBindViewHolder(final Adapter_Feed_Msg.ViewHolder holder, final int position) {

    Glide.with(mContext).clear(holder.ProfileImage);
    Glide.with(mContext).clear(holder.postImage);

 .
 .
 .
}
MobIT
  • 880
  • 6
  • 15