简体   繁体   中英

Glide issues on re opening + cannot load from destroyed activity

I've been using Glide in adapter where I pass context from Activity class to my fragment(View is in fragment). I've checked in around 30 devices and in 3-5 devices app crashes when the user tries to restart after closing. What could be the reason and how to handle this exception? I don't want to use Applicationcontext in Glide as this will keep the glide resources. Is there any other workaround?

Logcat

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.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:87)
at com.bumptech.glide.Glide.with(Glide.java:629)
at com.trial.project.Adapter.LiveUserListAdapterInside.onBindViewHolder(LiveUserListAdapterInside.java:68)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1618)

Below code for glide loading

Glide.with(itemHolder.ImageView.getContext())
    .load( current.getPhotoUrl() )
    .bitmapTransform(new CropCircleTransformation(context))
    .placeholder(R.drawable.x2)
    .into(itemHolder.friendImageView);

Below code for OnBackPressed()

    // ... ...
      ... ..
    finishAffinity();

Whenever you are trying to load an image I guess the activity wil be in destroyed state.This happens when you try to load an image and move to different activity before loading.So make a null check to see if still activity exists and also image reference exists.

Also replace itemHolder.ImageView.getContext() with itemHolder.friendImageView.getContext().

Check this edited code for loading image

 if(!this.isDestroyed() && friendImageView!=null && current.getPhotoUrl()!=null){
        Glide.with(itemHolder.friendImageView.getContext())
                .load( current.getPhotoUrl())
                .bitmapTransform(new CropCircleTransformation(context))
                .placeholder(R.drawable.x2)
                .into(itemHolder.friendImageView);  
    }

Always use the Activity context while loading images using Glide.

And while using Glide with adapters you can pass RequestManager object from activity itself in Constructor of adapter using Glide.with(this) .

It is always safe to check whether activity/fragment is visible or not before starting a Glide load.

So for Activity :

if(!this.isFinishing()){
   // Initialise Glide here
}

For Fragment :

MyFragmentClass mFragment = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (mFragment != null && mFragment.isVisible()) {
     // Initialise Glide here
}

Ref : https://android.jlelse.eu/using-glide-few-tips-to-be-a-pro-60f41e29d30a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM