App resuming results in crash with FormsAppCompatActivity

I have a Xamarin Forms app, currently being built for Android. I have a MainActivity that used to extend FormsAppCompatActivity. The app crashes whenever I switch out of the app and then back into the app. It throws an error in the App.xaml.cs class in the OnResume method, where I try to set the MainPage to a new navigation page:
protected async override void OnResume() { Task.Delay(10).Wait(); MainPage = new NavigationPage(new HomePage()); base.OnResume(); }
The error I'm getting is:
Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Aside from the error itself, my first thought is "Why are you doing it at all?"
    Just because someone backgrounded the app and resumed you're killing all the navigation history etc.?

    As a user that wouldn't make me very happy. I expect to be able to be on a page, switch apps to look something up, maybe check my password from a text document, copy some text, come back to an app and pick up where I left off - maybe pasting in something I copied from the other page or document. And I expect to be able to [BACK] through my history to previous pages in that app. and so on.

  • DharumaRajDharumaRaj USMember ✭✭

    How do you do that? I need to navigate to HomePage when app resumes? This is my requirement actually @ClintStLaurent

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited October 2016

    Just push the page.. Like you would push any other page when then user does something that requires a new page. But don't change the MainPage and don't make a whole new NavigationPage (and thus new Navigation stack.

    If all that is gibberish, and you don't understand how to navigate in Xamarin...
    Xamarin University course AND205 is "Android Navigation Patterns". IOS205 is the iOS Navigation class.

  • DharumaRajDharumaRaj USMember ✭✭

    I Get the Same error When I push other Page @ClintStLaurent

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Let's see the code for how you're doing that.

  • DharumaRajDharumaRaj USMember ✭✭

    protected async override void OnResume() { Task.Delay(10).Wait(); await MainPage.Navigation.PushAsync(new HomePage()); base.OnResume(); }
    @ClintStLaurent

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Same way I push pages, so I don't think it's your call to pushing the pages.

    The error I'm getting is:
    ```Java.Lang.IllegalStateException: Can not perform this action after

    onSaveInstanceState```

    When I search that error there are a lot of threads on it - mostly about how onSaveInstanceState doesn't get called correctly.
    http://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-after-onsaveinstanc

    What do you have happening when you go to sleep? Is there some other mechanism at work here for saving the state of your app when it backgrounds?

  • DharumaRajDharumaRaj USMember ✭✭

    No I don't save any state? Im not able to trace the problem

Sign In or Register to comment.