thinice
thinice

Reputation: 710

Xamarin Forms - PopToRootAsync and then PushAsync doesn't trigger

I'm using Xamarin Forms and I am trying to back out of the nav stack from a button command - and navigate to a new page (essentially, I want to "reset" the history on the back button).

_finishToolbarItem.Command = new Command(o => {
    Navigation.PopToRootAsync().ContinueWith(task => {
            Navigation.PushAsync(new StackOverflow());
        },
        TaskContinuationOptions.OnlyOnRanToCompletion
    );
}

I've tried doing this in an async command wrapper as well:

_finishToolbarItem.Command = new Command(async o => {
    await Navigation.PopToRootAsync();
    await Navigation.PushAsync(new StackOverflow());
});

Both of these approaches dump me to the root page - but the follow up PushAsync() is not working, despite it being called in the right order.

How can I "undo" the nav stack in Xamarin Forms (so the "back" buttons don't have anything to go back to) and push a new page?

Upvotes: 3

Views: 3721

Answers (2)

Lee Yeong Guang
Lee Yeong Guang

Reputation: 146

Try this:

Navigation.InsertPageBefore(new NewPage(),Navigation.NavigationStack[0]);
await Navigation.PopToRootAsync();

Upvotes: 0

Yuri S
Yuri S

Reputation: 5370

You could

InsertPageBefore (Page yourNewRootPage, Page firstPageInNavigationStack)

and then PopToRoot()

Upvotes: 2

Related Questions