Search code examples
xamarinxamarin.formsonresume

Navigate to default page from OnResume()


In Xamarin forms, the OnResume event handler is on the Application class, which is not a NavigationPage, so you cannot do any navigation in OnResume. When my app resumes, if it has been asleep for more than 30 mins, I want it to go back to its home page, not resume to the page where it went to sleep. I have got the 30 minute trap in the OnResume, but I can't do the navigation. Can anyone suggest a way of doing this?


Solution

  • by default, in a Forms app, you're storing the MainPage which should be a NavigationPage in this case.

    public class App : Application
    {
        public App ()
        {
            // The root page of your application
            MainPage = new NavigationPage {
    

    So you can use it to get to the Navigation object.

    protected override async void OnResume ()
    {
        var nav = MainPage.Navigation;
    
        // you may want to clear the stack (history)
        await nav.PopToRootAsync (true);
    
        // then open the needed page (I'm guessing a login page)
        await nav.PushAsync(new LoginPage());
    }