Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an activity stop, rather then be destroyed, from the BACK key?

Tags:

android

Right now an activity gets destroyed when the BACK key is pressed. How can I make it just stop ( i.e. keep all the variables, etc. alive ), rather then be destroyed?

Thanks!

like image 464
Roger Avatar asked Jul 22 '11 14:07

Roger


People also ask

Does Back button destroy activity?

To destroy activity on back press, use this code. //Destroys activity -> No, it does not.


3 Answers

Why is it that you need to keep the variables alive? Given the established lifecycle of an Android application, I'm not sure that preventing the activity from being destroyed "just to keep the variables" makes sense.

  1. Even if you stop the application without destroying it, there is always the chance that Android will kill it to free up memory. You will have to account for this in your code anyway, and so preventing the application from destroying doesn't save you from writing code.

  2. Variables can be saved and restored relatively easily and quickly using SharedPreferences in your onPause() and onResume() methods. Unless you are storing a ton of data, preventing the application from destroying might not make much of a difference.

  3. It sounds like you want to keep the variables in memory because you intend to return to this activity. Typically, you don't use the back button to navigate away from activities that you intend to come back to. Instead you would create an Intent and start a new activity. When you do this, Android places the current activity on the Back Stack calling onPause() and onStop(), which seems like exactly the sort of behavior you are looking for.

So if you still really want to prevent your activity from being destroyed (at least until Android decides it's using too much memory and kills it on it's own) you could always use Sagar's code and start a new activity in onBackPressed().

@Override
public void onBackPressed()
{
    Intent intent = new Intent(this, Other.class);
    startActivity(intent);       
}

Just be certain that that is what you really want to do.

like image 86
theisenp Avatar answered Sep 21 '22 15:09

theisenp


Simple one line

@Override
public void onBackPressed() {
    mActivity.moveTaskToBack(true);
}
like image 34
Atif Mahmood Avatar answered Sep 20 '22 15:09

Atif Mahmood


Pressing the BACK key triggers the onBackPressed callback method of Activity class. The default implementation of this callback calls the finish() method.

http://developer.android.com/reference/android/app/Activity.html#onBackPressed()

You can override this method to move the activity to background (mimick the action of pressing the HOME key.

eg:


@Override
public void onBackPressed() {
    onKeyDown(KeyEvent.KEYCODE_HOME);        
}

You could also instead consider moveTaskToBackground() mentioned here:

Override back button to act like home button

like image 32
Sagar Hatekar Avatar answered Sep 23 '22 15:09

Sagar Hatekar