3

In my app, I'm giving user the feature to set an image for the app's homescreen background(from gallery). The app works fine if the image if low resolution but if the user sets a high resolution image the app crashes on startup giving this error.

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@35f21ac is not valid; is your activity running?

I used the override method of glide to reduce the resolution of the image:

Glide.with(MainActivity.this).asBitmap()
    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate())
    .load(storageUtil.getWallpaperStatus())
    .apply(new RequestOptions().override(320,640).centerCrop())
    .into(viewPagerBackground);

Now, the crash frequency has reduced, but still sometimes the app crashes.

What could be the best solution for this problem?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Rektirino
  • 582
  • 5
  • 24

1 Answers1

0

paste your code within this code

if(!((Activity) context).isFinishing())
{
    //your code 
Glide.with(MainActivity.this).asBitmap()
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate())
                    .load(storageUtil.getWallpaperStatus())
                    .apply(new RequestOptions().override(320,640).centerCrop())
                    .into(viewPagerBackground);
}

This error occur sometime when we are loading image using glide but our activity is destroyed already.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42