ANDROID: activity state after clicking the back button

Imagine that you have the following sequence of actions:

Activity A -> Activity B -> Activity C 

When you are in action C, pressing the back button will take you to β€œActivity B.” What is the state of activity of C? Is he still in memory or was he finished?

If it is still in memory, is there a way to resume work? Besides launching another instance of this activity ...

I should add that this is a standard case when you do not use any flags, including: FLAG_ACTIVITY_CLEAR_TOP

+6
source share
4 answers

Perhaps you should consider reading the white papers .

In particular, the part that answers your question:

When the user presses the "Back" button, the current activity is set from the top of the stack (the action is destroyed), and the previous action resumes (the previous state of the user interface is restored).

Now for your second question ... you can continue to read the same page ...

when you start the action, you want to dump the existing instance (instead of creating a new instance on top of the back stack)

So, if you read this ... you will find ...

You can do this and more, with attributes in the manifest and flags in the intent that you pass to startActivity ().

In this regard, the main attributes you can use are:

taskAffinity launchMode allowTaskReparenting clearTaskOnLaunch alwaysRetainTaskState finishOnTaskLaunch

And the main flags of intent that you can use are:

FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP

+4
source

The default behavior is that when you press the hardware back button, the current activity will be removed from the back, and the kill sequence will be initiated. From now on, you should not rely on the fact that it can be somewhere nearby - it all depends on Android to decide when it really kills this activity.

My previous research shows that the onDestroy() victim will only be called when a new activity is executed and inactive.

You can specify android:launchMode="singleInstance" for your activity in the manifest. This ensures that only one instance of activity is created at a time.

+6
source

No, this is not in the memory. It ends when you click the back button. You should use android:launchMode="singleTask" in androidmanifest.xml for this particular action for which you do not want to create new instances if the instance already exists. For more information, this link will be helpful.

+4
source

In the following sequence ...

 Activity A -> Activity B -> Activity C 

When you press the back button in Activity C , then the onBackPressed() method will be called. The onBackPressed() behavior of onBackPressed() is to end the current activity until you can use the functionality of Override .

So, in the normal case, after clicking again, the current Activity will be destroyed, and you will not be able to find it in the Activity Stack , so you will not be able to resume it.

For more information, see the Android developer documentation from below ...

Tasks and Back Stack

+1
source

All Articles