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.28k stars 1.76k forks source link

Maui application does not restart properly after android activity is destroyed #18845

Open tranb3r opened 1 year ago

tranb3r commented 1 year ago

Description

When starting a Maui Application, the App constructor is called first, then the OnStart method is called. But if Android kills the Activity, then no method from App is called when relaunching the application. I would expect either the constructor or OnStart or OnResume to be called.

More details about events/methods being called during the lifecycle of an app (logs added to my repro):

Steps to Reproduce

  1. Open repro project
  2. Launch application. Check app logs.
  3. Press Finish to simulate android killing the activity. Check app logs.
  4. Restart the application. Check app logs.

Link to public reproduction project repository

https://github.com/tranb3r/Issues/tree/main/MauiAppLifecycleIssue

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No response

Relevant log output

No response

christianrr commented 11 months ago

This happens quite frequently on Pixel devices, as activities get temporarily closed for performance reasons (happens often overnight). If this happens, no app method is called for proper app recovery.

Probably there are some other recovery issues as well if activities are restored by OS. For instance, the Mopups PopupStack contains visible popups although the activity's DecorView is empty (= popup windows are gone).

.net MAUI 8.0.3, Android 13 & 14

christianrr commented 11 months ago

Our workaround is currently to call App.Current.Quit() in MainActivity's OnDestroy. Thus, we force a clean restart of the application including OnStart();

tranb3r commented 11 months ago

Our workaround is currently to call App.Current.Quit() in MainActivity's OnDestroy. Thus, we force a clean restart of the application including OnStart();

This is also what I'm doing.

ninachen03 commented 7 months ago

Verified this issue with Visual Studio 17.10.0 Preview 3 (8.0.20 & 8.0.0-rc.2.9530),I can repro it. image

PureWeen commented 6 months ago

I'm guessing the life cycle events you want are on the window

Try creating your own window type https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/windows?view=net-maui-8.0#create-a-window

And see about override the lifecycle methods on there

era-maui commented 4 months ago

We are also experiencing a problem when Android OS shuts down the activity. When it happens after the user has had the app in background for an extensive amount of time (6h to 20 days) then The MAUI App.cs still remembers the MainPage that was used last time, and does not reconstruct none oof the pages in current navigation stack. Is this intended and if it is, does this mean we need to manually restart all the platform dependent code when new Activity is created?