简体   繁体   中英

Xamarin.Forms A-B-C-A navigation not working for Android

So I created a PCL app with 3 pages: page 1, page 2 and page 3. Each page has Title Page 1, Page 2 or Page 3, and I put one button on each page. Then I write code as below to jump from page 1 to page 2 and then from page 2 to page 3 when clicking on the button.

On page 1 button click event handler:

    await Navigation.PushAsync(new Page2());

On page 2 button click event handler:

    await Navigation.PushAsync(new Page3());

Then in page 3 the button click event handler I use below the code nav back to page 1 directly by escaping page 2.

    Navigation.RemovePage(Navigation.NavigationStack.ElementAt(1));
    await Navigation.PopAsync();

The code works perfectly on iOS but not on Android. On Android it seems nav back to page 1 but page 1 view is not rendered, the title is gone and the button is gone so the view is basically blank. I tested on Nexus 4 API19 and API 22 emulator and the outcomes are same.

Can someone please help?

If you are only using the PushAsync to push pages into the navigation stack and there is no modals, tabbed pages, master detail page etc, then the best solution that is directly supported by Xamarin.Forms would be to use await Navigation.PopToRootAsync() . It would pop all the modals from your navigation stack except the Root. But it takes into consideration only the top most modal stack.

If in case your navigation stack is complex with modals, master detail, etc then I would suggest you to do it directly using the MainPage property than going the route you are trying to do (ie: Playing with Navigation service).

At first set the MainPage in App.cs to Page1

MainPage = new Navigationpage(Page1());

Do the same as you do in the events till you show up the Page3

In the Page3 button event do the following :

MainPage = new Navigationpage(Page1());

This would reset your Application's MainPage , both your Page3 and Page2 would be gone of the Navigation stack.

Drawback : The state of controls of Page1 wont be preserved, you will have to store it in some variables in the OnDisappearing event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM