31 Days of Windows Phone | Day #3: The Back Button Paradigm

This post is Day #3 in a series called the 31 Days of Windows Phone.

Yesterday, 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 “Jeff Blankenburg” from our list of contacts.
  3. Click on his home address (which takes us to the Maps application).
  4. Hit the Start button.
  5. Start playing a game.

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

Overriding the Back Button

Yes, you read that correctly.  You can override the Back button to make it do what you want.  This DOES NOT give you permission to be irresponsible with this power though.  In any case where you want to override the back button, you should be certain you’re considering what your user considers “back.”

For example, if you launch a pop-up style box, it’s possible the user’s muscle memory will have them push the Back button to get rid of it.  In that example, their intention isn’t to leave your application, instead “Back” meant to get rid of that popup.

In another example, if you’ve got a game that relies on a timer or live action, it might be appropriate to use the Back button to pause the game, rather than allowing them to leave immediately.  There’s a couple of guidelines you should follow when doing this however:

  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.

So here’s the code to override the Back button in Windows Phone 7.  Enjoy!

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

Tomorrow, we’re going to cover device orientation, and how we can handle Portrait vs. Landscape in our applications.  Until then…

13 thoughts on “31 Days of Windows Phone | Day #3: The Back Button Paradigm

  1. Jeff,

    How do you get to the People hub from the emulator?

    Thanks!

    John

    • Do you mind if I quote a few of your posts as long as I provide criedt and sources back to your website? My website is in the very same area of interest as yours and my users would really benefit from some of the information you provide here. Please let me know if this alright with you. Appreciate it!

  2. John,

    The emulator is for debugging applications that YOU have built. The emulator is not configured to be a full phone experience. Having said that, there are ways to interact with contacts on the phone, and I'll be providing ways to do on Day #8 and Day #9 in this series.

    Sorry I can't be more help on this one.

    • On notera que cette dame ne de9marre pas vearmint Windows et son UI mais simplement l’interface Web. Comme le disait votre idole, Steven Les rubans c’est la vie Sinofsky, l’interface metro style peut se de9marrer sans de9marrer le bureau Windows !Je tiens cependant a fe9liciter votre judicieuse remarque sur la stabilite9 dans le temps au fil des hibernations du kernel ! Et puisque l’on parle d’extinction comple8te, que se passe t’il si un nouveau pe9riphe9rique est ajoute9 entre le dernier de9marrage et le nouveau ? Voir la carte graphique change9e ?

  3. i put in the override for the back button but I am still able to go back.On the second click of the back button the event is fired. Do i need to do something else to prevent the user from using the back button

  4. Jawahar,

    If you have the e.Cancel = true in your overriden method, the back button should technically NEVER work. (But you should not be preventing your users from going back, only providing alternative means to do so.)

  5. All professions are conspiracies against the laity.

  6. You can never underestimate the stupidity of the general public.

  7. Happiness in intelligent people is the rarest thing I know.

  8. Science is nothing but trained and organized common sense, differing from the latter only as a veteran may differ from a raw recruit and its methods differ from those of common sense only as far as the guardsman's cut and thrust differ from the manner in which a savage wields his club.

  9. It is unbecoming for young men to utter maxims.

  10. Thanks for the article
    my Q is how to override Hold back button not the press event?

    • I don’t believe you are able to override the Back Hold gesture. That is an OS level feature that the user will expect to happen at all times. “Back” can have amorphous meaning, depending on the context of the app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s