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:
try to override void onBackPressed()
defined in android.app.Activity class.
I've tried all the above solutions, but none of them worked for me. The following code helped me, when trying to return to MainActivity in a way that onCreate gets called:
Intent.FLAG_ACTIVITY_CLEAR_TOP is the key.
@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
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.
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:
// 2.0 and above
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
// Before 2.0
@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.
Override onBackPressed() after android 2.0. Such as
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
if it helps someone else, I had an activity with 2 layouts that I toggled on and off for visibilty, trying to emulate a kind of page1 > page2 structure. if they were on page 2 and pressed the back button I wanted them to go back to page 1, if they pressed the back button on page 1 it should still work as normal. Its pretty basic but it works
@Override
public void onBackPressed() {
// check if page 2 is open
RelativeLayout page2layout = (RelativeLayout)findViewById(R.id.page2layout);
if(page2layout.getVisibility() == View.VISIBLE){
togglePageLayout(); // my method to toggle the views
return;
}else{
super.onBackPressed(); // allows standard use of backbutton for page 1
}
}
hope it helps someone, cheers