سؤال

Imagine you have the following sequence of activities:

Activity A -> Activity B -> Activity C

When you are on Activity C, pressing the native back button, takes you to Activity B. Now what is the state of Activity C? Is it still in memory or it has been finished?

If it is still in the memory, is there a way to resume the activity? Other than starting another instance of this activity...

I should add that this is the standard case where you do not use any flags including: FLAG_ACTIVITY_CLEAR_TOP

هل كانت مفيدة؟

المحلول 2

You might want to consider reading the official docs.

More specifically the part that answers your question:

When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).

Now for your second question… you can keep reading the same page…

when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack)

So if you read that… you will find…

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

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

taskAffinity launchMode allowTaskReparenting clearTaskOnLaunch alwaysRetainTaskState finishOnTaskLaunch

And the principal intent flags you can use are:

FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP

نصائح أخرى

Default behavior is that when you press hardware "back" button, current activity will be removed from the backstack and activity "destroy" sequence will be initiated. From that moment you should not rely on the fact that it might be somewhere around - it is all up to Android to decide when does it actually kill this activity.

What my previous investigations show is that victim's onDestroy() will be called only when new activity is done loading and is idle.

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

No, it is not in memory. It gets finished when you press the back button. You should use android:launchMode="singleTask" in the androidmanifest.xml for that particular activity for which you want no new instances to be created if an instance is already there. For further information this link will be helpful.

In the following sequence...

Activity A -> Activity B -> Activity C

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

So, in normal case, after back press the current Activity will be destroyed and you can't find it in the Activity Stack, so you can't resume it.

You will find more information from Android Developer's doc in below link...

Tasks and Back Stack

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top