user3599830
user3599830

Reputation: 25

Back button control in windows phone app

I am developing a windows phone 8 app. I want to control the back button of the phone for doing specific task. I want that when user press the back button in specific page it will not navigate to the previous page but to the page which I want. Is their any way to control the hardware back button present in phone?

Upvotes: 0

Views: 1311

Answers (2)

pumpkinszwan
pumpkinszwan

Reputation: 1352

In Silverlight apps (WP7, WP8, WP8.1) you do this:

protected override void OnBackKeyPress(CancelEventArgs e)
{
    // put any code you like here
    MessageBox.Show("You pressed the Back button");
    e.Cancel = true;                       
}

That will work in all Windows Phone versions if you're using Silverlight.


If you're using WinRT for Windows Phone 8.1, it is a bit different:

Open NavigationHelper.cs and make this modification:

private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (this.GoBackCommand.CanExecute(null) && !e.Handled)
    {
        e.Handled = true;
        this.GoBackCommand.Execute(null);
    }
}

Now in your app page (the page that will be open when the back button is pressed), add the following namespace:

using Windows.Phone.UI.Input;

Add this handler to the constructor method of your page:

HardwareButtons.BackPressed += OnBackPressed;

Then add this method:

private async void OnBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    e.Handled = true;
    // add your own code here to run when Back is pressed
}

Note: in both cases, the 'e.Handled = true' line tells the OS that the back button press has been handled, and therefore the OS will not action the default behaviour. If you remove that line your own code will run, and the OS will also do its own backwards navigation.

Be mindful of Rowland's comment about overriding the Back button - if you're not navigating intuitively you will confuse the user and risk your game being rejected (if you just need to control a pause screen or menu it will be fine, but if you implement something gimmicky like using the Back button as a game control you'll be in trouble).

My blog has the same answer with a bit more detail if you need it: http://grogansoft.com/blog/?p=572

Upvotes: 2

Rowland Shaw
Rowland Shaw

Reputation: 38130

Whilst it possible to cancel the navigation event, and permissable in a game to present a pause screen or similar, generally it is not allowed to use the back button for anything other than backward navigation in an app; Per requirement 5.2.4 of the Technical certification requirements for Windows Phone

To maintain a consistent user experience, the Back button must only be used for backwards navigation in the app.

If you are creating a XAML app where it is permissible to cancel a "back" operation, such as per 5.2.4.4 of the Technical certification requirements for Windows Phone :

For games, when the Back button is pressed during gameplay, the game can choose to present a pause context menu or dialog, or it can navigate the user to the prior menu screen.

Then you can implement this by overriding the OnNavigatingFrom method on your page, and set the Cancel property of the NavigatingCancelEventArgs, so something like this example from Frame, page, and navigation features for Windows Phone 8:

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    // If the navigation can be cancelled, ask the user if they want to cancel
    if (e.IsCancelable)
    {
        MessageBoxResult result = MessageBox.Show("Do you want to stay here?", "Confirm Navigation from Page", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
            // User wants to stay here
            e.Cancel = true;
            return;
        }
    }
}

Of course, you may choose to implement the prompt differently, but that should illustrate how it is possible.

Upvotes: 1

Related Questions