Om Infowave Developers
Om Infowave Developers

Reputation: 1565

You cannot start a load for a destroyed activity

MainActivity has below code for load GIF, the application gets a crash for load GIF after restart activity.

 ImageView imageViewGIF = navigationView.findViewById(R.id.imageViewGIF);
    Glide.with(this).asGif().load(R.drawable.gift_3).into(imageViewGIF);

Loading GIF in ImageView using glide

But when I restart the app due to some requirement using

 Intent intent = new Intent(context, SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();

Application get a crash with below log.

    FATAL EXCEPTION: main
 Process: com.apppackage, PID: 19360
 java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
     at android_support.qc.b(RequestManagerRetriever.java:298)
     at android_support.qc.a(RequestManagerRetriever.java:123)
     at android_support.ji.a(Glide.java:589)

I have used 'com.github.bumptech.glide:glide:4.2.0'

Upvotes: 2

Views: 6734

Answers (2)

clauub
clauub

Reputation: 1160

I don't know if this helps somebody, but for me was a struggle. I've used Glide.with(getApplicationContext()) but it didn't work.

So, I had to do an if condition to check if activity is Destroyed something like this:

  if (!isDestroyed) {
    Glide.with(getApplicationContext()) 
    ....
  }

This worked perfectly for me! Hope will be helpful for somebody

Upvotes: 2

Ashish Tiwari
Ashish Tiwari

Reputation: 2246

This is the issue in Glide library. You can check the context for not equals to null before loading Glide or you can use application context for this.

Glide.with(getApplicationContext())
  .load(imageUrlToLoad)
  .into(ImageView);

Upvotes: 2

Related Questions