Home > DeveloperSection > Forums > Windows Phone 8.1 Universal App terminates on navigating back from second page?
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    April-24-2015 1:25 AM

 Windows Phone Windows Phone 
Ratings:


 1 Reply(s)
 601  View(s)
Rate this:
I have 2 pages in my Windows Phone 8.1 Universal App.

I navigate from Page1.xaml to Page2.xaml by using a button with the click event code:

this.Frame.Navigate(typeof(Page2));

When I am on Page2, and I use the hardware back button the app closes without an exception or anything. It just returns to the startscreen.

I already tried the following on Page 2:

public Page2()
    {
        this.InitializeComponent();
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
    void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        Frame.GoBack();
    }


As far as I know I do not clear the back stack.

What is going on, and how can I fix this?



Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    April-24-2015 7:38 AM

This is new to Windows Phone 8.1.

If you create a new Hub Universal App using a VS2013 template, you'll notice a class in Common folder called a NavigationHelper.

This NavigationHelper gives you a hint how to properly react to back button press. So, if you don't want to use the NavigationHelper, here's how to get the old behavior back:
public BlankPage1()
{
    this.InitializeComponent();
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        e.Handled = true;
        Frame.GoBack();
    }
}

You can also do it on app level, to avoid having to do it on every page:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;
    #if WINDOWS_PHONE_APP
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    #endif
}
#if WINDOWS_PHONE_APP
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame != null && rootFrame.CanGoBack)
    {
        e.Handled = true;
        rootFrame.GoBack();
    }
}
#endif

Don't want to miss updates? Please click the below button!

Follow MindStick