安卓开发的Tasks and Back Stack
此文为阅读官方指南http://developer.android.com/guide/components/tasks-and-back-stack.html之后的理解
最简单的莫过于Back Stack,其实它就是一个栈,当你从一个Activity不断打开另外的一些Activity的时候,就不断的入栈,按退后键的时候就不断出栈。
操作的过程中还有主页键,当你按下去的时候并不代表所有栈就出栈出完了,事情并不是这么简单,下面就进行讲解。
当你启动了A应用之后,按下主页键的时候,当前A应用的的Task只是从foreground进入了background,并没有消失,此时当你打开B应用的时候,B应用的TASK进入foreground,此时你只能看到B无法看到A。
那么A应用的的Task没有消失的话,它何时进入foreground?
这就涉及到应用本身的设置了,开发者对应用进行相应的设置,一个Activity有以下的启动类型(这里只讲manifest设置的方法):
Using the manifest file
When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <activity>
element's launchMode
attribute.
The launchMode
attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode
attribute:
"standard"
(the default mode)- Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.
"singleTop"
- If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its
onNewIntent()
method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default
"standard"
launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is"singleTop"
, the existing instance of D receives the intent throughonNewIntent()
, because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is"singleTop"
.Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in
onNewIntent()
. "singleTask"
- The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its
onNewIntent()
method, rather than creating a new instance. Only one instance of the activity can exist at a time.Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
"singleInstance"
.- Same as
"singleTask"
, except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.
As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask
launch mode in the <activity>
element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.
Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Backbutton always takes the user to the previous activity. However, if you start an activity that specifies thesingleTask
launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.
For more information about using launch modes in the manifest file, see the <activity>
element documentation, where the launchMode
attribute and the accepted values are discussed more.
Note: The behaviors that you specify for your activity with the launchMode
attribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.
上面不同的设置,就会影响到background的应用回到foreground的方式。