Random geekery since 2005.

Husband, father, programmer, speaker, writer, blogger, podcaster, collector, traveler, golfer.

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…

Tags

13 responses to “31 Days of Windows Phone | Day #3: The Back Button Paradigm”

  1. John Stagich Avatar

    Jeff,

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

    Thanks!

    John

    1. Aymen Avatar

      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. jeffblankenburg Avatar

    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.

    1. Maiira Avatar

      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. jawahar rajan Avatar
    jawahar rajan

    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. jeffblankenburg Avatar

    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. get payday loans online Avatar

    All professions are conspiracies against the laity.

  6. Massachusetts payday loans Avatar

    You can never underestimate the stupidity of the general public.

  7. Rhode Island payday loans Avatar

    Happiness in intelligent people is the rarest thing I know.

  8. New Jersey payday loans Avatar

    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. Mississippi payday loans Avatar

    It is unbecoming for young men to utter maxims.

  10. Me 3bodd Avatar
    Me 3bodd

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

    1. jeffblankenburg Avatar
      jeffblankenburg

      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 )

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: