Android Back ButtonOne of the differences between Android devices and their biggest competitor, the iPhone, is the presence of several hardware buttons.  Android devices typically have the following hardware buttons: Menu, Home, Search, and Back.  We’ve already made use of the Menu button with the options menu we created on Day 6.  For today’s 31 Days of Android article, we’ll talk about the Back button and how to override it’s functionality.

Catching the Back Button Press

Intercepting a back button press is actually remarkably easy as of Android 2.0.  The code below is all you need to add to an activity to catch it:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Go ahead and add this to your ActivityTwo from the last day’s code.  Now when you press back, you will first go through this method.  As is, nothing different will happen.  If you comment out the super.onBackPressed() method and then rerun your application, hitting the back button won’t do anything.  You won’t return to the first activity, you won’t back out of the application.  Nothing.  If you wanted to prevent a user from backing out of your application, this is how you could do it.   I did say though, that this only works on 2.0 and later.  If you want to catch the back press prior to 2.0, you can use the onKeyDown method like so:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        //Do something on back press
    }
    return super.onKeyDown(keyCode, event);
}

Interestingly, if you override both the onBackPressed and onKeyDown, both will catch the back press with onKeyDown catching it first.  If you call super.onKeyDown in onKeyDown like we are above, then the onBackPressed method will fire.  If you do not call super.onKeyDown then onBackPressed will never be called.  Unless you have a specific reason to target below 2.0, there isn’t much of a reason to bother.  Pre 2.0 now accounts for less than 2.5% of devices connecting to the Android Market.

Can I I Really Stop a User from leaving my App?

Don’t forget that in addition to the back button, devices also have a Home button.  Furthermore, if you try to change expected behavior, users won’t like your app.  One of the big differences between Android apps and iPhone apps is that iPhone apps have to be reviewed and if things don’t perform like a normal “acceptable” application, it won’t pass review.  The Android Marketplace doesn’t have a review process like this, but users will be happy to tell you when they hate how your application works. 

So Why Use it?

Under most circumstances, the back button should function the same in your application as it does everywhere else.  That said, there are scenarios where it comes in handy.  First, if you want to change the default animation when a user taps back you could do so in the onBackPressed.  If you add an animation to the res/anim folder, you could change the animation played when going back between activities like so:

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
}

Here, we’ve replaced whatever the default back animation is with a zoom out and in.  You won’t be able to put this code into your onBackPressed button without creating the zoom_in and zoom_out animations.  Don’t worry too much about this as we’ll talk more about animations in a future article. 

A second reason for overriding the back button is a little more complicated to explain.  Let’s say your user interface has some element that comes on screen due to some user interaction.  An example of this is the Android camera.  When you load up the Android camera, the right side has a tab that, upon tapping, slides out with different options (Scenes, Effects, Flash, and Switch To (video recorder)).  When this tab pops out, it covers the Take Picture button:

Android Day Ten Camera

It’s relatively easy to tap anywhere in the camera frame to dismiss this tab, however, some users might feel that tapping the back button should remove the tab but not close the camera.  It’s quite possible you might have something similar in your application.  Instead of leaving your activity on the back button tap, you could check to see if the tab was visible, and then hide it.  Then on a second tap of the back button, you could back out of the activity as a user would expect.

Can I Assume Android Devices will all have a Back Button?

Starting with the tablet devices that came out with Android 3.0 on them, manufacturers started moving away from hardware buttons and instead had a button / status bar at the bottom of the Android OS screen.  It looks like Android and Google are continuing this with both phones and tablets in Ice Cream Sandwich.  So while future devices may not have a hardware back button like current devices do, they will still feature a software implemented back button.

We didn’t do a lot of coding today but you can download the latest source here.


Chris Risner