1

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();
        }
    }
Community
  • 1
  • 1
HansElsen
  • 1,639
  • 5
  • 27
  • 47
  • These are links to solution in Android (java code). I hope they may be useful to you or give you an insight at least: Solution 1: http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-wit Solution 2: http://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-after-onsaveinstanc Solution 3: http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html – Luis Beltran Apr 05 '16 at 09:57
  • Thanks, but I don't have any (easy) control over `Fragments` and `Transactions` in Xamarin Forms. – HansElsen Apr 05 '16 at 11:40
  • I'm having the same issue. Hopefully someone can answer this soon! – Kala J Aug 08 '16 at 01:06

0 Answers0