selva
selva

Reputation: 31

How to skip a page in back navigation in windows Phone 7

I am developing a application on windows phone 7. So my landing page is a custom Splash Screen page ( i didn't use the default way of showing a splash screen ie. having a png file, as i need to add data in the splash screen at runtime based on the user profile). So i created a splashscreen page. After a period of delay ( 5 sec), I navigate to my main page. using

PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
 root.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

Now, if the user presses the back button of the phone, by default to goes to the splash screen, but i want the application to exit.

How i can achive this. .

Upvotes: 2

Views: 2557

Answers (4)

Heena Mulla
Heena Mulla

Reputation: 196

Very simple just add this to page after the splash screen i.e, your 1st page

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        NavigationService.RemoveBackEntry();
    }

Here you remove the back entry to splash screen. On click of back your app will be killed instead of coming back to splash. Happy coding.

Upvotes: 0

Cœur
Cœur

Reputation: 38667

Unfortunately, jmason's solution will not work on Windows Phone 8 because you are not allowed to forcibly exit an application, which means you are not allowed to write this.NavigationService.GoBack(); on the splash screen. A better (and working) solution is to clean the navigation stack when you are done with the splash screen.

On the page after the splash screen (usually the main page) :

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    // Calling the base class OnNavigatedTo method
    base.OnNavigatedTo(e);

    // Only clear the stack for 'new' (forward) and 'refresh' navigations
    if (e.NavigationMode == NavigationMode.New || e.NavigationMode == NavigationMode.Refresh)
        // For UI consistency, clear the entire page stack
        while (this.NavigationService.RemoveBackEntry() != null)
            // Do nothing
            ;
}

Upvotes: 3

erickfiveten
erickfiveten

Reputation: 153

This solution creates a cleaner exit by hiding your splash screen before exiting the app. I use this code for to skip navigating back to a particular page.

Step 1: Add jmason's code

Step 2: Set default of LayoutRoot to hidden

Visibility="Collapsed"

Step 3: In the OnNavigatedTo event, show the LayoutRoot if the _navigateBack flag is false. In the OnNavigatedFrom event, re-hide the LayoutRoot

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (_navigateBack)
        {
            this.NavigationService.GoBack();
        }
        else
        {
            LayoutRoot.Visibility = Visibility.Visible;
            _navigateBack = true;
            base.OnNavigatedTo(e);
        }
    }
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        LayoutRoot.Visibility = Visibility.Collapsed;
    }

Upvotes: 0

jmason
jmason

Reputation: 143

I had a similar scenario and here is the fix I used.

For the page that I wanted to skip upon navigating back, I added a bool member variable that determines whether or not we load the page's content or navigate back to the previous page. This method works and doesn't appear to have any adverse affects on the application but there may be a better solution out there. Suggestions for a better method are welcomed.

public partial class LoginPage : PhoneApplicationPage
{
    private bool _navigateBack;

    public LoginPage()
    {
        InitializeComponent();

        _navigateBack = false;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (_navigateBack)
        {
            this.NavigationService.GoBack();
        }
        else
        {
            _navigateBack = true;
            base.OnNavigatedTo(e);
        }
    }
}

Upvotes: 4

Related Questions