Java.Lang.IllegalArgumentException: You cannot start a load for a destroyed activity

ac-lap 71 Reputation points

I am building an eBook reader app (primarily targeting android). The app displays list of books (image + text). But whenever the app is 're-initialized' (I am using it for lack of my understanding of correct term), all the images in the app stop loading with the below error.

This 're-initialization' I have noticed happening following scenarios -

  1. The app is running, toggle the Android system theme - Dark/Light theme (Happens only on some devices, not all, API 33)
  2. The app is running, and it receives an intent. Following is the intent filter defined -
    [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = "com.xx.yy")]
  3. The app is the background for very long, when brought to foreground.

In all these cases OnCreate() is called in the MainActivity.

From the error in the Glide library, looks like it continuing to use the old activity instance. How can I re-initialize Glide library to use the new activity? or how can I fix this issue, or any workaround?

I am using # 2 above for fetching Google API OAuth token, and upon login the app is wrecked which is very poor experience. It happens on both debug and release build and multiple devices.

Unable to load image file '/data/user/0/com.xx.yy/files/CoverImage/a5c6f357-0458-4db1-9999-74387c2ee156.jpg'. --> 
Java.Lang.IllegalArgumentException: You cannot start a load for a destroyed activity

java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
	at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(
	at com.bumptech.glide.manager.RequestManagerRetriever.get(
	at com.bumptech.glide.manager.RequestManagerRetriever.get(
	at com.bumptech.glide.Glide.with(
	at Method)
	at android.os.Handler.handleCallback(
	at android.os.Handler.dispatchMessage(
	at android.os.Looper.loopOnce(
	at android.os.Looper.loop(
	at java.lang.reflect.Method.invoke(Native Method)

Please suggestion some fix or workaround for this, currently this is blocking issue for my app.

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,148 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 58,011 Reputation points Microsoft Vendor


    Please set the LaunchMode = LaunchMode.SingleTask, for your MainActivity, it will do not create new activity when your application in the background.

     [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.