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:
- Open the People hub.
- Select “Jeff Blankenburg” from our list of contacts.
- Click on his home address (which takes us to the Maps application).
- Hit the Start button.
- 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:
- You should consider what “back” means to your user at that point in time.
- If the user presses the Back button a second time, you should let the default behavior happen.
- 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…
Leave a Reply to New Jersey payday loans Cancel reply