Mavese
Mavese

Reputation: 131

How to determine what the back button does in universal apps

I am writing a universal app and when I am testing it on the windows phone emulator when the back key is pressed it just brings me back to the start screen instead of navigating back a page.

This is the first windows phone 8.1 app I have made and I need some help on how to set so that the back key takes you back an app page instead of bringing you out of the app.

Upvotes: 1

Views: 369

Answers (1)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21899

You need to handle the HardwareButtons.BackPressed event and plug into your app's navigation system. Commonly you'll find the Frame object, check if frame.CanGoBack, and if so call frame.GoBack. If you're at the app's front page (frame.CanGoBack is false) then don't handle the event and let it back out of the application.

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame frame = Window.Current.Content as Frame;
    if (frame == null)
    {
        return;
    }

    if (frame.CanGoBack)
    {
        frame.GoBack();
        e.Handled = true;
    }
}

See Handling the Back button in a Windows Phone app

The NavigationHelper.cs classes in the non-blank Windows Phone app templates will hook this up for you.

Upvotes: 1

Related Questions