2
votes

As far as I’ve seen, if the host native Activity is recreated, the Xamarin Forms Pages are recreated. This can be easily tested if I enable “Don’t keep activities” on the device. I expected the NavigationPage to retain the back stack with the Xamarin Forms Pages.

How can I retain the back stack? Do I have to implement my own NavigationPage? What options do I have with current version of Xamarin Forms?

3

3 Answers

2
votes

if you remember push and pop operation in stack then this image will give you all you need

visual representation of page navigation in xamarin forms

1
votes

Andrei,

Make the root view controller your NavigationPage you can push as many ContentPage(s) as you would like. The Navigation Page is keeping the stack of views being pushed automatically and then you can Pop them to your rootview ContentPage or previous ContentPage(s)...

var mainNav = new NavigationPage(new BoxedView());

then to push:

Xamarin.Forms.Device.BeginInvokeOnMainThread(() => 
    Navigation.PushAsync(CONTENT_PAGE_OBJECT, false));

to pop:

Xamarin.Forms.Device.BeginInvokeOnMainThread(() => Navigation.PopAsync(true));
1
votes

Here you can manage the Navigation stack

// Adding pages to Navigation stack

Xamarin.Forms.Current.MainPage = new NavigationPage(new Page1());
Xamarin.Forms.Current.MainPage.Navigation.PushAsync(new Page2(), false));
Xamarin.Forms.Current.MainPage.Navigation.PushAsync(new Page3(), false));

// Removing pages from Navigation stack

var page3 = Xamarin.Forms.Current.MainPage.Navigation.PopAsync(false));
var page2 = Xamarin.Forms.Current.MainPage.Navigation.PopAsync(false));
var page1 = Xamarin.Forms.Current.MainPage.Navigation.PopAsync(false));