Summary
Xamarin.Forms currently is tied to the lifecycle of an the MainActivity. This means if that activity is destroyed in anyway (OS kills it, etc) the app "restarts" and offers no way to handle this behavior or restore state. This is summarized in bug #29275.
API Changes
Copying the information from the linked bug report, this is a suggestions:
Google's recommendation for this is to use a retained fragment. The activity looks for the fragment in its fragment manager when it's created, and if it doesn't find one it creates a new one. All important state is kept in the fragment, and the activity does almost nothing. That's what we need here. Instead of
FormsApplicationActivity
we needFormsApplicationFragment
.
Intended Use Case
A common example where the current behavior is undesirable is when the app is backgrounded to the task manager. Then, some time later the user resumes from the task manager and the app restarts. That is, MainActivity.OnCreate
is called again and the entire LoadApplication
process happens, effectively losing any state that the user might have had in the app.