Handling the “Back” Button with a Popup Open on Windows Phone 7

Using Popups in your application is a great way to present the user a nice “Yes/No” question, or provide some sort of temporary content. However, it does provide an additional challenge to handling the device’s “Back” button. According to Microsoft guidelines when the user presses the “Back” button the app should “Ensure the application closes the current screen in focus, and returns the user to the previous page.” In other words, pressing the “Back” button, should just close your popup window and not the whole page. Failure to implement this has resulted in our apps failing the approval process of 5.2 Performance and Resource Management of section 5.2.4.b. In next section technical detail of certification failure is described.

5.2.4.b Performance and Resource Management Requirements

Pressing the Back button must return the  application to the previous page.

Expected Result

Test Process Required:

1. Launch the application.
2. Navigate through the application.
3. Select the device Back Button.
4. Ensure the application closes the current  screen in focus, and returns the user to the previous page.
5. Pressing the device back button may alternatively show a menu or a dialog box that the application surfaces.

Steps to reproduce:

1. Launch the application.
2. Select the author’s name and then select “Bio”.
3. Press the back button on the device.
4. Notice the application does not return to the previous page and it terminates after pressing the back button twice.
5. This error is reproducible 10 out of 10 times.

Using OnBackKeyPress To Handle Popup

To overcome 5.2.4.b you would need to override the OnBackKeyPress event as shown in below code. First check if your Popup IsOpen and set it to false if open and then set e.Cancel to true so you can cancel the back button.

protected override void OnBackKeyPress

(System.ComponentModel.CancelEventArgs e)

{

//Check if the PopUp window is open

if (MyPopupWindow.IsOpen)

{

   //Close the PopUp Window

   MyPopupWindow.IsOpen = false;

 

   //Keep the back button from

   //navigating away from the current page

   e.Cancel = true;

}

else

{

   //There is no PopUp open, use the back button normally

   base.OnBackKeyPress(e);

}

}

 

Conclusion

You learn about how to handle Popup windows when the user clicks the back button on the phone and avoid the Marketplace certification failure.

About toetapz

Henry Lee is founder of NewAgeSolution.Net and is passionate about the technology. He is also the author of the book Beginning Windows Phone 7 Development 2nd from Apress (http://www.apress.com/9781430235965). He works with various Fortune 500 companies delivering mobile applications and rich internet applications. He recently formed start-up company called ToeTapz.com focusing his energy on delivering mobile applications in Windows Phone 7, Android and iPhone. In his spare time, he dedicates his effort to help his .NET community by delivering sessions at the technology events. He enjoys talking with other technologist about current trends in the technology and sharing business insights with fellow colleagues. Often you will find Henry at local cigar bar enjoying a cigar and a drink trying to come up with next big mobile application.
This entry was posted in Marketplace Certification, Windows Phone 7 and tagged , . Bookmark the permalink.

One Response to Handling the “Back” Button with a Popup Open on Windows Phone 7

  1. Pingback: How to create Reusable About Page and learn about NavigationService, WebBrowserTask, MarketplaceDetailTask, and Page Transition with Animation? | Toetapz's Blog

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 )

Connecting to %s