Roger
Roger

Reputation: 6527

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

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!

Upvotes: 18

Views: 14905

Answers (7)

Aravind.HU
Aravind.HU

Reputation: 9472

Just specify in the manifest for the activity as

 android:persistent="true"

That should prevent your activity getting destroyed. To know more about this please refer to these below links which were answered by me

How to prevent call of onDestroy() after onPause()?

Prevent activity from being destroyed as long as possible

In the above posts I have explained in detail with a use case

Upvotes: -4

Hoang Nguyen Huu
Hoang Nguyen Huu

Reputation: 1252

I have managed to work out exactly what you want: switch between 2 activities using Back button and keep them all not to be destroyed!

For example: you have 2 activities A & B. A will be started first, then A calls B. When B is loaded, user press Back button and switches back to activity A from B. From now B should not be destroyed and just goes to background, and when user starts activity B from A again, activity B will be brought to foreground, instead of being re-created again or created new instance!

How to implement this:

1. Override onBackPressed() of activity B:

@Override
public void onBackPressed() {
    Intent backIntent = new Intent(this, ActivityA.class);
    backIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(backIntent);
}

2. On activity A, call activity B:

public void callActivityB() {
    Intent toBintent = new Intent(this, ActivityB.class);
    toBIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(toBintent);
}

remember to add flag: Intent.FLAG_ACTIVITY_REORDER_TO_FRONT when you call A&B.

Upvotes: 2

theisenp
theisenp

Reputation: 8719

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.

Upvotes: 14

Atif Mahmood
Atif Mahmood

Reputation: 8992

Simple one line

@Override
public void onBackPressed() {
    mActivity.moveTaskToBack(true);
}

Upvotes: 14

miroslavign
miroslavign

Reputation: 2023

First of all, sorry for not answering the question, cause, I still have no optimal answer for it. But, I really like when people start asking "what do you need this for". And, very rarely, the person who asked the question, really deserves this kind of question. I think not this time, but ok, this is not the issue...

Anyway, I will try to point out why some of us are convinced that

going from Activity A to Activity B(creating UI based on some data fetching) AND

going back from B to A(destroying all the created UI and/or fetched data in B) is sometimes a bad concept. Better solution would be to keep the stack as it is, so using something like finish() in Activity B, but keeping the Activity B in Pause state, so later when calling it again from Activity A - it just goes in onResume = nothing recreated in UI, no additional data fetching. The bonus of course is a fast and responsive UI, and the difference is really if you have a more complicated UI layout.

Upvotes: 0

Sagar Hatekar
Sagar Hatekar

Reputation: 8790

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

Upvotes: 7

Jonathan Pitre
Jonathan Pitre

Reputation: 2425

This is similar to this question that was asked earlier.

Hope this helps! N.S.

Upvotes: 1

Related Questions