Reopening the app from background is crashing with an exception of "Window was already created" from base.onresume().
When I try to reopen an app from background it is crashing when I am using base.onresume() in mainactivity. It gave an exception "Window was already activated". I saw some work around of adding launchmode as singletop or singletask would work but in my case it didnot work. I added OnNewIntent method also to see if works, but no. I tried many ways making it to work but it didnot. Please suggest some solution for this.
Thanks in advance.
.NET MAUI
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,566 Reputation points • Microsoft Vendor
2024-05-09T06:20:05.5833333+00:00 For further investigation, could you please provide the following information?
- The Maui version and android api level.
- Could you please provide specific steps to reproduce and update this into the issue?
- MAUI officially provides the App lifecycle method, in which the Activated method corresponds to the Android Onresumed method. Will I get an error when using this method?
Your prompt reply will be highly appreciated.
-
Keerthana Krishna 60 Reputation points
2024-05-09T07:13:47.33+00:00 MAUI Version- 8
Android API Level-29
Steps:
1.Create MAUI Project and in mainactivity class add OnResume method.
- Run the app and put the app in background by clicking on home button.
- Reopen the app from background and it gives an exception of "Window was already activated".
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,566 Reputation points • Microsoft Vendor
2024-05-10T02:36:37.9333333+00:00 I tried the test using the same runtime environment and steps and the result is that the Application opens as expected without crashing.
To investigate further, could you please provide information on how you override the OnResume method. And whether this issue occurs if you use the official MAUI provided lifecycle method?
-
Keerthana Krishna 60 Reputation points
2024-05-10T04:17:21.9433333+00:00 Stack Trace:
at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Activated() in D:\a_work\1\s\src\Controls\src\Core\Window\Window.cs:line 496at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_2(Activity activity) in D:\a_work\1\s\src\Core\src\Hosting\LifecycleEvents\AppHostBuilderExtensions.Android.cs:line 34
at Microsoft.Maui.MauiApplication.ActivityLifecycleCallbacks.<>c__DisplayClass2_0.<OnActivityResumed>b__0(OnResume del) in D:\a_work\1\s\src\Core\src\Platform\Android\MauiApplication.cs:line 110
at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEventsOnResume in D:\a_work\1\s\src\Core\src\LifecycleEvents\LifecycleEventServiceExtensions.cs:line 31
at Microsoft.Maui.MauiApplication.ActivityLifecycleCallbacks.OnActivityResumed(Activity activity) in D:\a_work\1\s\src\Core\src\Platform\Android\MauiApplication.cs:line 110
at Android.App.Application.IActivityLifecycleCallbacksInvoker.n_OnActivityResumed_Landroid_app_Activity_(IntPtr jnienv, IntPtr native__this, IntPtr native_activity) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Application.cs:line 735
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 125
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,566 Reputation points • Microsoft Vendor
2024-05-13T02:08:11.3666667+00:00 I have rewritten these three methods according to your images and tested them following the steps and environment you provided, and the application did not crash.
Therefore, this may be a context-specific issue.
Does this issue still occur if you start testing from a new template project?
-
Keerthana Krishna 60 Reputation points
2024-05-13T13:13:54.1533333+00:00 Yes I am still facing this issue.
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode =LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]I tried using Launchmode as Singletop, singletask, but it did not work. I tried without Launcmode as well, even that it didnot work. Please provide some solution to this.
Thanks
-
David Brewer 0 Reputation points
2024-05-13T13:19:48.1133333+00:00 Hello,
It appears you're encountering app crashes when reopening it from the background due to issues with the base.onresume() method in MainActivity. The exception Window was already activated suggests a conflict in activity lifecycle or window management. Try experimenting with different adpvantage launch modes in the AndroidManifest.xml file for MainActivity to see if it resolves the problem. Ensure that the onNewIntent() method is correctly configured to handle incoming intents. Use logging statements to track the execution flow and debug the issue.
Thanks!
-
Keerthana Krishna 60 Reputation points
2024-05-23T11:20:45.7466667+00:00 I tried adding different launch modes with and without intent, but it never worked.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,566 Reputation points • Microsoft Vendor
2024-05-24T05:33:04.2333333+00:00 Thanks for your feedback.
I have tested using the various properties you mentioned, and the crash did not occur when the Application was reopened from the background.
For the issue you are experiencing, it is more likely that some environment configuration is causing the issue. Have you tried reinstalling the emulator, updating the Nuget Package, and rebuilding the project to reproduce the issue?
-
Manickam, Suraj 320 Reputation points
2024-05-28T09:22:11.22+00:00 Hi Liu,
Even I faced the same issue and tried everything that was suggested but it did not resolve the issue , One thing I noticed is in Xamarin, LoadApplication gets executed during a platform specific launch and in here app.xaml.cs itself has its own lifecycle and it gets called multiple times after the app resumes from background pushing the platform specific onResume() to be called and hence I got this error.
Sign in to comment