Ric
Ric

Reputation: 83

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?

Upvotes: 6

Views: 6887

Answers (2)

Benoit Jadinon
Benoit Jadinon

Reputation: 1161

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());
}

Upvotes: 11

pnavk
pnavk

Reputation: 4630

Try updating your OnResume method to look like this:

protected async override void OnResume ()
{
    await MainPage.Navigation.PopToRootAsync (true);
}

Upvotes: 3

Related Questions