'Remove page from Navigation stack - xamarin.forms

In my app structure is like below, List Page -> Detail Page -> Edit Page

and in edit page there is button "Delete" which removes data from database.

Now my problem is to navigate user from Edit page to List Page,

I'm using Navigation.popasync 2 times for this, but on detail page i'm getting error from service that no such record exist.

How can i properly navigate user from Edit page to list page?



Solution 1:[1]

Navigation.PopToRootAsync ();   

Sends you back to your main page..

Solution 2:[2]

You can do like this:

var _navigation = Application.Current.MainPage.Navigation; 
var _lastPage = _navigation.NavigationStack.LastOrDefault(); 
//Remove last page
_navigation.RemovePage(_lastPage);
//Go back 
_navigation.PopAsync();

But if you need to navigate to the root page you can use this:

var _navigation = Application.Current.MainPage.Navigation;
_navigation.PopToRootAsync ();  

Solution 3:[3]

That will be because your detail page is trying to load a record that no longer exists on an OnAppearing or some other event. As such just put a condition at the top of if the record is null to not load the page.

Solution 4:[4]

Hope this help someone!

var PreviousPage = Shell.Current.Navigation.NavigationStack.Count - 2;

var PageYouWantCheck= typeof(NameOfPage);

if(Shell.Current.Navigation.NavigationStack[PreviousPage].GetType() == PageYouWantCheck)
{
     var pageToRemove = Shell.Current.Navigation.NavigationStack[PreviousPage];
     Shell.Current.Navigation.RemovePage(pageToRemove);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mr.Koçak
Solution 2 ata
Solution 3 Adam
Solution 4 Ricardo Ferreira