ZZZ
ZZZ

Reputation: 2812

In Xamarin.Forms Android app the MainActivity.OnCreate is called again after pressing the device Back button then app icon?

In Xamarin.Forms Android app the MainActivity.OnCreate is called again after pressing the device Back button to exit app then pressing app icon or snapshot in the app running list to launch again. Thus, all states are lost though the app seems to remain in the app running list. If I press the device middle button to exit then press the app icon to launch again, I see the apps keep all states since MainActivity.OnCreate is not called, which has some initialization codes.

Question 1: Why there's difference between exiting app via Back button and via Middle button? While both ways have the app still running.

Question 2: Is it worthy/legitimate/conventional to make both ways provide the same app behaviors -- keeping all states when being activated again?

Question 3: If yes, how to do correctly?

Upvotes: 1

Views: 716

Answers (1)

cy-c
cy-c

Reputation: 855

Please refer to: https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/

And: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/app-lifecycle

Question 1: Why there's difference between exiting app via Back button and via Middle button? While both ways have the app still running.

In XF, the back button can serve multiple purposes on android such as popping pages from the navigation stack or exiting and killing the app. Although the app still appears backgrounded after pressing back, it is actually terminated. The middle button puts the app in the background.

Question 2: Is it worthy/legitimate/conventional to make both ways provide the same app behaviors -- keeping all states when being activated again?

This question is subjective, meaning it depends on what you are trying to do. But generally, no. It is important to adhere to Android OS best practices because users expect the buttons on their device to behave in a similar fashion to how they behave in the OS.

https://developer.android.com/distribute/best-practices/develop/

Question 3: If yes, how to do correctly?

See links above. This is also a great resource: https://learn.microsoft.com/en-us/xamarin/

Upvotes: 1

Related Questions