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

ac-lap 71 Reputation points
2023-11-13T13:26:35.4766667+00:00

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(RequestManagerRetriever.java:353)
	at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:153)
	at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:217)
	at com.bumptech.glide.Glide.with(Glide.java:902)
	at com.microsoft.maui.PlatformInterop.loadImageFromFile(PlatformInterop.java:244)
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:31)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7884)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

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

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,500 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,581 Reputation points Microsoft Vendor
    2023-11-17T09:47:36.84+00:00

    Hello,

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.