Passing Windows Phone Certification – The Back Button

23 במאי 2012

Tip #2: How not to break Windows Phone certification rule No. 5.2.4 – Use of Back Button.


So this is a rather common one, mostly because developers don’t take the hardware keys into consideration when developing an application, or on the contrast use them incorrectly. 

Basically what it means is: Make sure the Back Button does what it’s supposed to do: Navigate Back. No matter what your application does, no matter how many transitions occur, the back navigation needs to be fluid, logical and uniform with all other application on the platform.

 

Windows Phonw Hardware Buttons

 

Getting back to scratch:
At any point during the lifecycle of your application the user might press one of the phone’s three Hardware Buttons: The Start Button, The Search Button and The Back Button.
Every device will have them as depicted in the Hardware Specifications Microsoft has set for manufacturers.

This means that when contemplating use cases for your application, these three possible events should be at the top of the list. 
It’s not as complex as it might sound, The Start and Search Buttons currently act quite the same on the developer’s side, by that I mean that you can not override their behavior (Rightfully for the Start Button, not so rightfully for the Search Button). So for these two you only need to handle the phone navigating away from your app, which for you means: Tombstoning, but more on that on another time.

The Back Button on the other hand, that fella can take a bit of abuse.
As I hinted before, the Back Button is the only hardware key out of the three that can have it’s behavior overridden.
And that’s where you meet The Good, The Bad and The Ugly.

The Good:
Overriding the Back Button is easy, it only takes 2 simple steps.
 
Step #1: On your application page register to BackKeyPress event.
You can do that from code behind, or the way I prefer to do it, in XAML.

Registering to BackKeyPress in XAML

Step #2: In code behind implement the said event handler.
In a fashion similar to this:

private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
{
   // Override the back press, actually canceling it
    e.Cancel = true;

   /*                                                     *
     *  Things to do instead of navigating  *
     *                                                    */
}

We cancel the back navigation command and execute our own logic.
Obviously implementing this event handler is purely optional, and should be avoided if not needed.

A good example for where you should use it would be to control dialogs, popups and custom messages boxes. Which leads me to…

The Bad:

Let’s say your application page presents at one point or another some sort of Popup, Dialog, Context Menu, or any other type of partial window which lays on top of the page, and will stay there until the user interacts with it.

According to Certification rule 5.2.4.3 – Back Button: Context Menus and Dialogs -If said control is presented on the page, the Back Button should close it instead of navigating back to the previous page.
How good this is considering UX is arguable, but the rules are the rules.

The way to fix this would be creating an event handler, just as we saw before, and in it checking if we have our dialog control open. If it is, cancel the back navigation and close the dialog, if it isn’t leave the back navigation to do it’s thing.
Translated into code, it will look like this:

private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)

            if (dialog.Visibility == Visibility.Visible)
            {
                // Override the back press
                e.Cancel = true;

                // Close the dialog
                dialog.Visibility = Visibility.Collapsed;
            }
}

The Ugly:

So now you know how to override the back button.
This means you can use it to do what ever you like, right?
Wrong!

To explain this, imagine the next scenario:
You’re a Facebook user right? Of Course you are. 
Now let’s say that one day the good developers at Facebook find a way to override the the browser’s navigation buttons (this by the way is very feasible using some rather simple Javascript).
So from now on whenever you’re on Facebook, clicking on the Back browser button will open the chat window, and clicking on the Forward browser button will “Like” the last post you’ve watched.  

How long do you think it will take before Mark Zuckerberg’s mail account will reach it’s storage limit after being flooded by hate-mail?
And for a good reason, this makes for a very confusing, non intuitive, user experience.

The same happens when the Back Button on the phone does something that isn’t some sort of navigating back.
As I said on my first post about Windows Phone Certification, most of the rules were made to maintain UX across the board, and this also means that all hardware buttons should more or less act the same on all applications.

Or to sum it up in Microsoft’s words:
“To maintain a consistent user experience, the Back button must only be used for backwards navigation in the application. ”

That’s it for now.
More Windows Phone Certification Tips coming. Please tell me in the comment section about certification rejections you’ve got so I’ll know what you want me to write about next.

Josef.   

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

3 comments

  1. Mike24 במאי 2012 ב 18:03

    When submitting my first windows application, I had two failures one for the icon that pins the application to the start page and one for having some portion of the test tool showing in my application png's. The bad part was as soon as the icon failed the testing stopped and I was told I had one failure. I fixed the icon and was notified about the next failure, which cost me another 5 days of certification time. It would have been nice to know about both failures at the same time.

    This is more of an FYI statement to everyone. Your failure report may not be a complete test run. It appears once a failure is found the testing stops.

    Reply
  2. Priyank Kothari26 במאי 2012 ב 7:20

    I am trying to close my application by pressing back button. it closes the application if the button is pressed first time after the game is loaded. but when I click on refresh button, which loads the page again and than when I try to close the application with back button. it actually reloads the main page instead of closing the application. any suggestion on that ?

    Reply
  3. Josef Goldstein28 במאי 2012 ב 22:04

    @Priyank Kothari

    When you say "Refresh Button" do you mean a custom button in your application that preforms re-navigation to the page?

    If that is the case it actually makes sense, if you think about it the navigation history will look like this:

    Start screen -> Game page -> Game page("refreshed")

    So one Back navigation will indeed get you back to the main page.

    The solution here is rather simple: don't use navigation to reset a page, reset it using code behind.

    Reply