Thomas Joulin
Thomas Joulin

Reputation: 6650

Remove a page from Navigation Stack

I have this application schema :

[List Page] -> [Detail Page] -> [ShareOnFacebook Page]
                     ^__________________|
  1. The user select an Item in the [List Page]
  2. The user may or may not click a Share button if he or she does, the application navigates to a [ShareOneFacebook page] which displays a Facebook Login Form, posts a message and navigates back
  3. The user navigates back.

If the user shared on facebook, it will "repost" the message, because the application store the session so it will navigate back to ShareOnFacebook, and then back to my Detail page.

If the user didn't share, he goes back to the List Page.

How can I "ommit" the ShareOnFacebook page in my navigation stack ?

Upvotes: 13

Views: 10769

Answers (6)

Rico Suter
Rico Suter

Reputation: 11858

Try this: Call NavigationService.RemoveBackEntry(); in the OnNavigatedTo method. This will remove the previous page from the stack. In my opinion the trick with Navigation.GoBack(); is not satisfying because it shows the page to remove for a short time.

Note: Works only with Windows Phone OS 7.1 (Mango) SDK

Upvotes: 26

Grigory
Grigory

Reputation: 1922

Have a look at simple library i wrote for such purposes: http://navcoerce.codeplex.com/

var fluent = new FluentNavigation(RootFrame);                          

fluent.WhenNavigatedTo<MainPage>()                                     
      .ThenTo<LoginPage>()                                             
      .ThenToAnyPage()                                                 
      .RemoveEntriesFromBackStack(1);                                  

fluent.WhenNavigatedTo<MainPage>()                                     
      .ThenTo<LoginPage>()                                             
      .ThenTo<RegisterPage>()                                          
      .ThenTo<PaymentPage>()                                           
      .RemoveEntriesFromBackStackTill<MainPage>();                     

fluent.WhenNavigatedTo<MainPage>()                                     
      .ThenTo<SecondPage>()                                            
      .ThenTo<RegisterPage>()                                          
      .ThenOptionallyTo<ForgotPasswordPage>()                          
      .ThenToAnyPage()                                                 
      .RemoveEntriesFromBackStackTill<MainPage>();                     

fluent.WhenNavigatingTo<PaymentPage>()                                 
      .RedirectTo<LoginPage>();                                        

fluent.WhenNavigatingTo<PaymentPage>()                                 
      .If(() => false)                                                 
      .RedirectWithReturnUri<LoginPage>("ReturnUri");   

Upvotes: 3

Chidi Ekeocha
Chidi Ekeocha

Reputation: 11

I use the removeBackEntry method on the NavigationService Class. I also use this as a way to setup my own splash screens

        private void BWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // switch screen 
        NavigationService.Navigated += new NavigatedEventHandler(NavigationServiceNavigated);
        NavigationService.Navigate(new Uri("/Pages/main.xaml", UriKind.Relative));

    }

    void NavigationServiceNavigated(object sender, NavigationEventArgs e)
    {
        NavigationService.RemoveBackEntry();
    }

Upvotes: 1

jmason
jmason

Reputation: 143

I posted an example for the same issue here.

The Nonlinear Navigation Service Matt linked to essentially does the same thing but would probably be better than my simple example.

Upvotes: 0

Amr H. Abd Elmajeed
Amr H. Abd Elmajeed

Reputation: 1521

I have a similar situation in my app, i solve it with a very simple solution.

If you want to "skip" a page in your backstack, place some logic in the NavigatedTo() function of that page.

For example: you can have a bool that you set to true when you post to facebook, and then place the following code in the NavigatedTo() function of the ShareOnFacebook page.

Here is pseudo code:

if (alreadyPosted) Navigation.GoBack();

The GoBack() function will skip this page and return to the previous one, the user will never see the page.

Upvotes: 3

Related Questions