Navigate to a new page without putting current page on back stack?

navigation, windows-phone-7

Solution

When navigating to the NewPage.xaml pass along a parameter so you know when to remove the previous page from the backstack.

You can do this as such:

When navigating from CurrentPage.xaml to NewPage.xaml pass along parameter

    bool remove = true;
    String removeParam = remove ? bool.TrueString : bool.FalseString;

    NavigationService.Navigate(new Uri("/NewPage.xaml?removePrevious="+removeParam , UriKind.Relative));

In the OnNavigatedTo event of NewPage.xaml, check whether to remove the previous page or not.

    bool remove = false;

    if (NavigationContext.QueryString.ContainsKey("removePrevious"))
    {
        remove = ((string)NavigationContext.QueryString["removePrevious"]).Equals(bool.TrueString);
        NavigationContext.QueryString.Remove("removePrevious");
    }

    if(remove)
    {
        NavigationService.RemoveBackEntry();
    }

This way, you can decide on the CurrentPage.xaml if you want to remove it from the backstack.

Problem

In an Windows Phone 7 application I got a CurrentPage, which, on a special event does navigate to a new page using the NavigationService: ``` NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative)); ``` Now when the user clicks back on the NewPage I want the app to skip the CurrentPage and go directly to the MainPage of the app. I tried to use NavigationService.RemoveBackEntry, but this removes the MainPage instead of the CurrentPage. How do I navigate to a new page without putting the current on the back stack?

Original source