I've a FormsAppCompatActivity
and it is crashing every time I turn on my screen while the app is active.
These are the steps I undertake:
- Start debugging -> app shows nicely
- Turn off screen -> app still attached to debugger
- Turn on screen -> app crashes after setting the MainPage property of the Application
I have a timeout that after 30s of idleness, the app should call the Logout()
method and return to the login page.
When I suspend the app by pressing the home button, this code still works, when I resume the app without turning screen on&off.
I already tried setting a delay in the OnResume
like here: App resuming results in crash with FormsAppCompatActivity
It has something to do with Fragments
and Transactions
being commited after state loss, but I don't know how to fix it.
The exception being thrown is Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
In MyApp.cs:
public MyApp ()
{
Start(message: string.Empty);
}
private void Start(string message) {
var mainPage = GetMainPage();
MainPage = mainPage; //<-- this crashes
userService.LoggedOut += (sender, e) => {
ThreadPool.Start(() => {
Start (e.Message);
}, ThreadPoolType.Ui);
};
}
protected override void OnSleep ()
{
base.OnSleep ();
sleepDateTime = DateTime.Now;
}
protected override void OnResume () {
base.OnResume();
var delta = DateTime.Now - sleepDateTime;
if (delta.TotalSeconds > SECONDS_FOR_TIMEOUT) {
userService.Logout();
}
}