Credit to Steven, http://www.sticapac.com for allowing me to post this up on my blog.
Recently, I worked on an app for the Windows Phone 7 platform and submitted it to the marketplace for certification. I’ve learnt many things and gained many insights after going through this process and would like to share one of these with you, developers and potential developers of the Windows Phone 7 platform.
Throughout this post, I will be making references to the official Windows Phone 7 Application Certification Requirements document (WP7ACR) which can be downloaded athttp://go.microsoft.com/?linkid=9730558.
Under Section 5.2.4 of the WP7ACR, clause a, it is stated that:
Pressing the Back button from the first screen of an application must exit the application.
Developers who are creating apps with multiple pages must give special attention to this clause because pressing the back button automatically brings the user to the previous page, regardless of whether the user is at the first screen or not. Consider the following example:
In this app, there are 2 pages. Page1 goes to Page2 via a button with the following logic:
NavigationService.Navigate(new Uri(“/Page2.xaml”, UriKind.Relative));
Page2 goes back to Page1 via a button with the following logic:
NavigationService.Navigate(new Uri(“/Page1.xaml”, UriKind.Relative));
Each time that line of code is executed, it is actually creating a new instance of the page defined under the new Uri. Therefore if the user navigates in this manner:
At this point in time, the user is at the first page of the application. If the user presses the back button, he will be brought back to Page2 instead of exiting the app, which is wrong and will result in a failure of the certification process.
There is no API in the Windows Phone 7 SDK similar to the Application.Exit() function found in the average C#.NET Windows Forms application. For this reason, we must overcome this problem by navigation by page hierarchy. That is to say, when the user navigates from Page2 to Page1 via the button, instead of treating Page1 as a new page, the developer should instead use this line of code for the navigation purpose from Page2 back to Page1:
By doing this, the developer is essentially coding the button to function exactly like what the back button does on the Windows Phone. In this way, the app will always exit when the back button is pressed on the first page because the first page is always the only record under the action history when the user is on the first page. The diagram will then look like this:
Therefore we have successfully adhered to Section 5.2.4 of the WP7ACR, clause a. This is how we can effectively use the Back button and pass certification for functions relating to the back button.