04. The Back Button

Series LogoIn the previous post, we learned how to pass parameters through NavigationService in a Windows Phone Application.

This is the 4th post in “Discover Windows Phone” development series  we’ll be covering the back button paradigm.

A couple of posts ago, we talked about Page Navigation, and how we can simply call the NavigationService to get to a different page in our application.  The Back button was mentioned briefly, but the complexities of the Back button are deserving of their own post in this series, primarily because you can override what the Back button does.

How the Back Button Works

Think of the Back button much like the one in your browser.  You can click it to navigate backwards through the history of decisions you have made in that session.  It takes you across multiple sites, and every page you visited within those sites.  The Back button on a Windows Phone works the same way.  It will take you backwards through the pages you have visited, even across multiple applications!

So, for example, if we:

  1. Open the People hub.
  2. Select a friend from our list of contacts.
  3. Click on you’re friends website
  4. Hit the Start button.
  5. Open another application.

When we hit the back button, we will navigate backwards through the previous list of events.

Overriding the Back Button

In some cases, overriding the back button is needed. In Windows Phone development you can override the Back button to make it do what you want. In any case where you want to override the back button, you should be certain you’re considering what your user considers “back.”

  1. You should consider what “back” means to your user at that point in time.
  2. If the user presses the Back button a second time, you should let the default behavior happen.
  3. Using the Back button to pause a game is perfectly acceptable, but a second press means they want to actually go backwards.  Let them.

Here you go. Here’s the code to override the Back button in Windows Phone 8.

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
//Your code goes here. Be responsible.
e.Cancel = true; //Cancels the default behavior.

That was the code for overriding the back button, next post will be discussing how to create an appBar
For any feedback or questions: