by brad
14. September 2010 06:00
By now, everyone following Windows Phone 7 knows that each phone has three prominent hardware buttons: Back, Start, and Search. The Back button is special though, in that your application can listen for it’s use and execute code in response.
1: <phone:PhoneApplicationPage
2: x:Class="WindowsPhoneApplication1.MainPage"
3: BackKeyPress="PhoneApplicationPage_BackKeyPress">
But with this power also comes responsibility. It would be bad manners to hijack the Back button for own nefarious means. To that end, the official Application Certification Requirements specify rules governing the use of the Back button. The good news is that if your application does not explicitly intercept Back button presses, all of these things will just work. For free.
- When the user presses the back button, they must be returned to the previous page.
- If the user is on the first page of your application and they press the back button, the application must exit.
- If your application is displaying a dialog or context menu, pressing the back button must close the dialog or context menu.
Again, if you do not write code to intercept the Back button event, all of these rules will just work for free. It’s only when you want to execute custom code in response to the Back button that you have to worry about making sure the rules are still being followed.
One example of a valid circumstance for executing custom code when the back button is pressed is to launch a custom animation before navigating. Can you think of any others?
Other Resources
More Tips