android – You cannot start a load for a destroyed activity in relativelayout image using glide-ThrowExceptions

Exception or error:

I am using relativelayout to set an image.Why I hadn’t using imageview means, inside relativelayout image, I am setting icons.

I dont know what is the issue exactly in glide.I have posted the stacktrace and relevant code below:

Logcat:

 FATAL EXCEPTION: main
   Process: com.app.steve, PID: 15928 
 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:644)
                                                                    at com.app.steve.TabMorePagesDetailActivity$allPageDetails.onPostExecute(TabMorePagesDetailActivity.java:1050)
     at com.app.steve.TabMorePagesDetailActivity$allPageDetails.onPostExecute(TabMorePagesDetailActivity.java:885)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
   at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

TabMorePagesDetailActivity.java:

RelativeLayout rlPageCoverImg;

rlPageCoverImg = (RelativeLayout)findViewById(R.id.rl_club_cover_img);

@Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);

            dialog.dismiss();
        ............

    String coverIMGurl = cover_avatar_obj.getString("url");

    Log.e("ImgURL", coverIMGurl);

 Glide.with(TabMorePagesDetailActivity.this).load(coverIMGurl).asBitmap().signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                                        .into(new SimpleTarget<Bitmap>(500, 500) {

    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
    Drawable drawable = new BitmapDrawable(getResources(), resource);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                            rlPageCoverImg.setBackground(drawable);
    }
    }
    });

    }else {

    rlPageCoverImg.setBackgroundResource(R.drawable.bg_golive);

    }



    @Override
 protected void onDestroy()
 {
    super.onDestroy();
    Glide.clear(rlPageCoverImg);

 }

layout.xml:

 <RelativeLayout
            android:id="@+id/rl_club_cover_img"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/cancel_image" >

  // Inside this relativelayout image, I'm using buttons and icons


 </RelativeLayout>
How to solve:

Use:

Glide.with(getApplicationContext()).load(...)

Instead of:

Glide.with(TabMorePagesDetailActivity.this).load(...)

Hope it will solve your problem~

###

You can simply check the context is destroyed or not manually as;

if (context == null) {
    return
} else if (context !is Application) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        if (context is FragmentActivity) {
            if ((context as FragmentActivity).isDestroyed) {
                return
            }
        } else if (context is Activity) {
            if ((context as Activity).isDestroyed) {
                return
            }
        }
    }
}

This can also be represented as a Kotlin extension function:

/**
 * Return true if this [Context] is available.
 * Availability is defined as the following:
 * + [Context] is not null
 * + [Context] is not destroyed (tested with [FragmentActivity.isDestroyed] or [Activity.isDestroyed])
 */
fun Context?.isAvailable(): Boolean {
    if (this == null) {
        return false
    } else if (this !is Application) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (this is FragmentActivity) {
                return !this.isDestroyed
            } else if (this is Activity) {
                return !this.isDestroyed
            }
        }
    }
    return true
}

###

I have got the same issue before few days.I have solved this to passing the Application context memory behalf of current Class context memory.

May be it will help you :-

use this code

 Glide.with(getApplicationContext())
           .load(coverIMGurl)
           .asBitmap()
           .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                                    .into(new SimpleTarget<Bitmap>(500, 500) {....}

Even you are getting this issue then read this article carefully “https://github.com/bumptech/glide/issues/1097

overview for this issue : This is an issue of Glide library.

###

Try this before load your image with Glide, in my case mirefer is a StorageReference, miimagen is a ImageView. I solved this problem, with this. I hope it could help you.

if (!this.isFinishing ()) {
                // Load the image using Glide
                Glide.with(YourActivity.this)
                        .using(new FirebaseImageLoader())
                        .load(mirefer)
                        .into(miimagen);
            }

###

Setup Glide with a parameter which has the correct lifecycle. For example: use Glide.with(this) instead of Glide.with(getContext()) in a custom view.

###

Inspired from a GitHub thread, I am using this 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;
}

###

the other way is to check if the activity is destroyed or not, then load into UI element.

if (!newActivty.isDestroyed()){
   ...
}

###

In activity use Gliide.with(getApplicatonContext()) and
In adapter use Glide.with(myContext.getApplicatonContext())

//It works for me fine.

//for adapter

Context myContext;

//also initilize in oncreateViewHolder

@Override
    public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_messages_layout, parent, false);
        myContext = parent.getContext();
        return new MessagesHolder(view);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *