Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash upon resuming the app 'window was already activated' #21948

Closed
ESO-ST opened this issue Apr 19, 2024 · 5 comments · Fixed by #23360
Closed

Crash upon resuming the app 'window was already activated' #21948

ESO-ST opened this issue Apr 19, 2024 · 5 comments · Fixed by #23360
Assignees
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/iOS 🍎 t/bug Something isn't working
Milestone

Comments

@ESO-ST
Copy link

ESO-ST commented Apr 19, 2024

Description

when we minimize the app and then restore it, we get a 'window was already activated' crash.

System.InvalidOperationException: 'Window was already activated'
   at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Activated()
   at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_3(UIApplication app)
   at Microsoft.Maui.MauiUIApplicationDelegate.<>c__DisplayClass14_0.<OnActivated>b__0(OnActivated del)
   at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnActivated](IServiceProvider services, Action`1 action)
   at Microsoft.Maui.MauiUIApplicationDelegate.OnActivated(UIApplication application)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at MyProject.Platforms.iOS.Program.Main(String[] args) in C:\siren\SES\MyProject\Platforms\iOS\Program.cs:line 12

This takes place when we are showing a popup page, and then close this popup page (with a slight delay) and immediately minimize the app then restore it.
Please see the repro project and follow the repro steps.

Steps to Reproduce

  1. open a popup page (press the "Open Popup Page 1" button)
  2. close the popup page and minimized the app (press the "Close popup page after a 2 seconds delay" button and immediately minimize the app)
  3. restore the app

Link to public reproduction project repository

https://github.com/ESO-ST/WebViewCrashMidLoad/tree/WindowWasAlreadyActivatedCrash
https://github.com/dotnet/maui/tree/fix_21948

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

no

Relevant log output

No response

@ESO-ST ESO-ST added the t/bug Something isn't working label Apr 19, 2024
@mikeparker104 mikeparker104 added the partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with label Apr 19, 2024
@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Apr 22, 2024
@XamlTest
Copy link
Collaborator

Verified on VS 17.10.0 Preview 4.0(8.0.20). Can repro this issue on iOS 17.2.
WebViewCrashMidLoad.zip

@samhouts samhouts added migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/iOS 🍎 labels Apr 22, 2024
@PureWeen
Copy link
Member

PureWeen commented Apr 25, 2024

I've added the repro to our sandbox project here

https://github.com/dotnet/maui/tree/fix_21948

The bug here is that our code that searches for the keywindow doesn't work because mopups is using it's own UIWindow

image

So when our life cycle code tries to search for the keywindow it doesn't return anything.

Some possible thoughts on fixes

  1. in a single window scenario (no SceneDelegate) just locate the one UIWindow
  2. instead of tying lifecycle events to the delegate events can we inherit from UIWindow and tie activate/deactive to events inside UIWindow instead so that it's a little closer to the source?

we should probably do one regardless if two is a better solution for this scenario

@PureWeen PureWeen modified the milestones: Backlog, .NET 8 SR5, .NET 8 SR6 Apr 25, 2024
@PureWeen
Copy link
Member

MopupsCrash.zip

Workaround

@PureWeen PureWeen self-assigned this May 1, 2024
@viswanath-pamarthi
Copy link

viswanath-pamarthi commented May 13, 2024

@PureWeen
Observed the same issue on iOS only with mopups, tried to use your work around. This is not working when UIWindow from binding library is the key window, as IWindow.Activated is not triggered when base.OnActivated(application) is called in AppDelegate(after minimize and try to bring app to foreground). As the window created by maui is not activated in previous scenario, we will see an exception in OnResignActivation that the window is already deactivated.

@viswanath-pamarthi
Copy link

viswanath-pamarthi commented May 14, 2024

@PureWeen

I have added below in OnActivated, this is helping as a work around for scenario mentioned in previous comment:

public override void OnActivated(UIApplication application)
{
            base.OnActivated(application);

            if (Application.Windows.FirstOrDefault() is App.WindowFix windowFix && !windowFix.IsActivated)
            {                
                (windowFix as IWindow).Activated();                
            }
}

@PureWeen PureWeen added the area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events label May 31, 2024
@PureWeen PureWeen modified the milestones: .NET 8 SR6, .NET 8 SR7 Jun 7, 2024
kubaflo added a commit to kubaflo/maui that referenced this issue Jun 29, 2024
@samhouts samhouts removed s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 3, 2024
kubaflo added a commit to kubaflo/maui that referenced this issue Jul 4, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 4, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 4, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 5, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 5, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 5, 2024
github-actions bot pushed a commit to kubaflo/maui that referenced this issue Jul 5, 2024
PureWeen added a commit that referenced this issue Jul 5, 2024
* [iOS] Crash upon resuming the app - fix (#21948)

* Refactor

* Update UIApplicationExtensions.cs

* Update UIApplicationExtensions.cs

* Update UIApplicationExtensions.cs

* Added a UI test (#21948)

* Added a test category (#23428)

* remove "'"

---------

Co-authored-by: Shane Neuville <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/iOS 🍎 t/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

6 participants