Sam
Sam

Reputation: 28999

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

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?

Upvotes: 6

Views: 3524

Answers (3)

akalucas
akalucas

Reputation: 476

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.

Upvotes: 11

Dominik Kirschenhofer
Dominik Kirschenhofer

Reputation: 1205

Where do you have called "NavigationService.RemoveBackEntry()"? I think you have to do it at the new page, not at the page you want to skip!

edit: So to get a better picture: you are having mainpage -> 1rst sub Page (should be skipped at back navigation) -> 2nd sub page which is independed from 1rst sub page.

2 ideas: 1) Try to call "NavigationService.RemoveBackEntry()" in the OnNavigatedFrom-Event of the 1rst sub page 2) Check in the OnNavigatedTo-Event of the 1rst sub page if the NavigationMode (see event args) == Back and navigate back once more.

Upvotes: 1

Edward
Edward

Reputation: 7424

It sounds like your calling RemoveBackEntry to early (While you're still on CurrentPage.xaml). Thats why its removing MainPage.xaml. When you navigate to NewPage.xaml, in the OnNavigatedTo event call NavigationService.RemoveBackEntry and that should fix the problem.

Upvotes: 0

Related Questions