Removing items from Xamarin.Forms navigation back stack

I’m currently hacking together a small utility/calculator app for my girlfriend and wanted to enable navigation from one page to another, but remove the page I was navigating from from the navigation back stack. Like I’ve masterly illustrated below:

remove-from-navigation

 

Navigating backwards from page 3 should send the user directly to page 1 and skip page 2 entirely.

My first instinct was to just remove an element from the back after navigating to the next page, but in Xamarin.Forms the back stack is an IReadOnlyList and can’t be edited.

After some googling I found a simple solution in the Xamarin forums: Insert the next page before the current page in the stack by using InsertPageBefore() and then navigate backwards:

Navigation.InsertPageBefore(new Page(), Navigation.NavigationStack[Navigation.NavigationStack.Count -1]);
await Navigation.PopAsync();

I’m guessing there are some weird cross platform compatibility issue with removing stuff from the back stack, but here’s a simple workaround at least 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s