Your App
class extends from Xamarin.Forms.Application
and the Xamarin.Forms.Application
life cycle is bind with the Activity
life cycle. You could find it in the source code :
async Task OnStateChanged()
{
if (_application == null)
return;
if (_previousState == AndroidApplicationLifecycleState.OnCreate && _currentState == AndroidApplicationLifecycleState.OnStart)
_application.SendStart();
else if (_previousState == AndroidApplicationLifecycleState.OnStop && _currentState == AndroidApplicationLifecycleState.OnRestart)
_application.SendResume();
else if (_previousState == AndroidApplicationLifecycleState.OnPause && _currentState == AndroidApplicationLifecycleState.OnStop)
await _application.SendSleepAsync();
}
So you just need to care about your Activity
's life cycle. When you touch the HomeButton
your application don't remove the MainActivity
from the task's stack and the MainActivity
will be available when you re-enter the app, so it didn't execute the OnCreate
method.
When you touch the HomeButton
:
[0:] MainActivity OnPause
[0:] MainActivity OnStop
[0:] Forms App OnSleep
When you use the Android TaskManager
open your application :
[0:] MainActivity OnRestart
[0:] Forms App OnResume
[0:] MainActivity OnResume
But when you touch the hardware BackButton
, the MainActivity
will be removed from the task's stack :
[0:] MainActivity OnPause
[0:] MainActivity OnStop
[0:] Forms App OnSleep
[0:] MainActivity OnDestroy
When the MainActivity
is destoryed, your application will dispose resource and it including your Gtue.Mobile.App
instance. And you could see the source code about OnDestroy() method :
protected override void OnDestroy()
{
PopupManager.Unsubscribe(this);
_platform?.Dispose();
// call at the end to avoid race conditions with Platform dispose
base.OnDestroy();
}
So the next time when you open your application, it is necessary to recreated the App
class. Actually, the App
did only be initialized once.