dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Android Crashes with "This window is already associated with an active Activity" when we use AppFlip #23645

Closed RobertHassfeld closed 3 months ago

RobertHassfeld commented 3 months ago

Description

We used an OAuth-based App Flip linking to connect to GoogleHome in our App. account-linking/app-flip-overview

Maybe this issue is related to: MAUI Android build crashes when app is reopened from background. It throws the exception: 'Window was already created.

Add better exception if user opens second activity

Steps to Reproduce

Perform the following steps for testing:

  1. Starts the identity-appflip-tester-android tool
  2. Add the following parameter to the string.xml file
  1. Start the AppFlip application
  2. Open the Android Tester Tool
  3. Click on "Try to flip"
  4. The flip will open the AppFlip application and crash
  5. Open the Android Tester Tool again
  6. Click again on "Try to flip"
  7. This time the AppFlip application should open in the Android Tester Tool

Link to public reproduction project repository

https://github.com/RobertHassfeld/AppFlip/tree/main

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12, 13, 14

Did you find any workaround?

No workaround found.

Relevant log output

[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: This window is already associated with an active Activity (AppFlip.MainActivity). Please override CreateWindow on AppFlip.App to add support for multiple activities https://aka.ms/maui-docs-create-windowor set the LaunchMode to SingleTop on AppFlip.AppFlipActivity.
at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\ApplicationExtensions.cs:line 52
at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.cs:line 35
at AppFlip.AppFlipActivity.OnCreate(Bundle savedInstanceState) in /Users/rhassfeld/Projekte/TestApps/AppFlip/AppFlip/Platforms/Android/AppFlipActivity.cs:line 35
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:line 3082
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
github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

RobertHassfeld commented 3 months ago

Further information. The CreateWindow function does not work because OnCreate is called with bundle = null and then CreateWindow is triggered. All necessary information is available in the CreateWindow method, but then the OnCreate method throws this exception and all information in the Intent is removed.

See this PR: Add better exception if user opens second activity

PureWeen commented 3 months ago

you need to remove this line

https://github.com/RobertHassfeld/AppFlip/blob/main/AppFlip/App.xaml.cs#L9

and instantiate your window inside createwindow

return new Window(new AppShell())

RobertHassfeld commented 3 months ago

Thank you for the solution. It works in my test project. Unfortunately, it has a big impact on our project. So we have to change the whole initialization process of the app.