views:

11314

answers:

2

On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state.

In the Android docs it states:

...not all activities have the behavior that they are destroyed when BACK is pressed. When the user starts playing music in the Music application and then presses BACK, the application overrides the normal back behavior, preventing the player activity from being destroyed, and continues playing music, even though its activity is no longer visible

How do I replicate this functionality in my own application?

I think there must be three possibilities...

1) Capture the back button press (as below) and then call whatever method(s) the home button calls.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
    }
    return super.onKeyDown(keyCode, event);
}

2) Capture the back button press and then spoof a home button press.

3) Capture the back button press, then start an Activity of the home screen, effectively putting my application's Activity into the stopped state.

Thanks in advance for any pointers.

Edit: I know about services and am using one in the application to which this problem is related. This question is specifically about putting the Activity into the stopped state rather than the destroyed state on pressing the back button.

+10  A: 

Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading.) If that's the case, then your Activity can finish as usual and the Service will still be running.

A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion.

NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

Mirko Nasato
moveTaskToBack() appears to work as desired. What might the downsides be to using this method? Are there cases where this might not work as expected?
bdls
After reading its doc more carefully, I actually think it should work fine. (I initially thought it applied to the activity, but in fact it says the "task containing this activity".) But you should test it for yourself of course. :)
Mirko Nasato
Thanks Mirko, the method appears to work well. It might be cool if you give more prominence to your edit at the bottom of your answer for those looking at this question later - as that was the bit I was looking for!
bdls
Please read http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html for the recommended way to handle the back key.
hackbod
@Mirko I agree that the back button should work as normal 99% of the time, but for Audio Players it makes sense to only destroy the app when audio is no longer being played. This means the activity retains state and appears as when the user left it when they return (and they will return to turn it off).The Music Player mentioned in docs will use a service for the audio, but it's perfectly feasible that they retain Activity state, for the reason I mention above, as well. I am genuinely interested in people's opinions on this...
bdls
@bdls in theory your background activity could be killed by the system if it runs low on resources, so to be safe it should be able to recreate itself anyway. I had a look at the source code for the Android Music app and don't see any special back button handling.
Mirko Nasato
+6  A: 

If you want to catch the Back Button have a look at this post on the Android Developer Blog. It covers the easier way to do this in Android 2.0 and the best way to do this for an application that runs on 1.x and 2.0.

However, if your Activity is Stopped it still may be killed depending on memory availability on the device. If you want a process to run with no UI you should create a Service. The documentation says the following about Services:

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.

These seems appropriate for your requirements.

Dave Webb