maui icon indicating copy to clipboard operation
maui copied to clipboard

Maui application does not restart properly after android activity is destroyed

Open tranb3r opened this issue 2 years ago • 4 comments

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):

  • start application
App: Create
Lifecycle: OnCreate
Lifecycle: OnStart
App: OnStart
Lifecycle: OnResume
  • application goes to background
Lifecycle: OnPause
App: OnSleep
Lifecycle: OnStop
  • application resumes
App: OnResume
Lifecycle: OnRestart
Lifecycle: OnStart
Lifecycle: OnResume
  • destroy activity
Finish activity
Lifecycle: OnPause
App: OnSleep
Lifecycle: OnStop
Lifecycle: OnDestroy
  • restart application ( <-- issue here )
Lifecycle: OnCreate
Lifecycle: OnStart
Lifecycle: OnResume

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

tranb3r avatar Nov 17 '23 15:11 tranb3r

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 avatar Dec 13 '23 12:12 christianrr

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

christianrr avatar Dec 13 '23 14:12 christianrr

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.

tranb3r avatar Dec 13 '23 14:12 tranb3r

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

ninachen03 avatar Apr 16 '24 09:04 ninachen03

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

PureWeen avatar May 31 '24 16:05 PureWeen

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?

era-maui avatar Jul 29 '24 11:07 era-maui