Skip to content

You cannot start a load for a destroyed activity #1484

Closed
@huzhenjie

Description

@huzhenjie

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)

Activity

TWiStErRob

TWiStErRob commented on Sep 26, 2016

@TWiStErRob
Collaborator

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.

shivangbtech

shivangbtech commented on Oct 18, 2016

@shivangbtech

Getting Same Problem.

xiaojidonggong

xiaojidonggong commented on Dec 14, 2016

@xiaojidonggong

.... crash , why not log ?

AAverin

AAverin commented on Jan 9, 2017

@AAverin

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

pankaj89

pankaj89 commented on Jan 10, 2017

@pankaj89

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

steve111MV

steve111MV commented on Jan 12, 2017

@steve111MV

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.

Emeritus-DarranKelinske

Emeritus-DarranKelinske commented on Mar 24, 2017

@Emeritus-DarranKelinske

What is a workaround for this issue?

Emeritus-DarranKelinske

Emeritus-DarranKelinske commented on Apr 6, 2017

@Emeritus-DarranKelinske

I fixed this by using getApplicationContext()

steve111MV

steve111MV commented on Apr 6, 2017

@steve111MV
Emeritus-DarranKelinske

Emeritus-DarranKelinske commented on Apr 6, 2017

@Emeritus-DarranKelinske

Can you please provide an effective alternative then?

TWiStErRob

TWiStErRob commented on Apr 6, 2017

@TWiStErRob
Collaborator

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

Emeritus-DarranKelinske

Emeritus-DarranKelinske commented on Apr 7, 2017

@Emeritus-DarranKelinske

Will check it out. Thank you! :)

23 remaining items

AhmadullahSaikat

AhmadullahSaikat commented on Feb 3, 2019

@AhmadullahSaikat

Please use Glide.with(getApplicationContext()) instead of Glide.with(this)

TWiStErRob

TWiStErRob commented on Feb 3, 2019

@TWiStErRob
Collaborator

Please don't use application context, use Glide.with(this) in onCreate to 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

houchenl

houchenl commented on Aug 28, 2019

@houchenl
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;
}
ali20kp

ali20kp commented on Aug 24, 2020

@ali20kp
added a commit that references this issue on Feb 6, 2021
rbenza

rbenza commented on Mar 17, 2022

@rbenza

I came up with this solution:

private fun RequestManager.syncWithLifecycleOwner(view: View): RequestManager {

    val syncRequest = object : DefaultLifecycleObserver {
        override fun onStart(owner: LifecycleOwner) = onStart()
        override fun onStop(owner: LifecycleOwner) = onStop()
        override fun onDestroy(owner: LifecycleOwner) {
            onDestroy()
            owner.lifecycle.removeObserver(this)
        }
    }

    view.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(syncRequest)

    return this
}

You then can simply call:

fun ImageView.loadUrl(url: String) = Glide.with(this).syncWithLifecycleOwner(this).into(this)

findViewTreeLifecycleOwner() is present in AndroidX Lifecycle lib. It provides the Activity or the Fragment View's lifecycle this specific ImageView is attached to.

See: https://stackoverflow.com/a/71503953/11310951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @korchix@guilmarc@sreejithraman@AAverin@xiaojidonggong

        Issue actions

          You cannot start a load for a destroyed activity · Issue #1484 · bumptech/glide