首页 > Android技术 > Android中task介绍
2011三月30

Android中task介绍

在Android平台上可以将task简单的理解为幽多个Activity共同协作完成某项应用,而不管Activity具体属于哪个Application,通过下图可以更清晰的理解Application、task、Activity三者之间的关系:

Android加载activity有四种不同的模式(通过android:launchMode属性设置),文档中是这么描述的:

“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 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 is deliverd the intent through onNewIntent(), 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 key to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the BACK key 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 key 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.

“standard”模式相信大家都很熟悉,没必要在介绍了,而”singleTop”和”standard”相似,差别是如果singleTop的activity在该task的顶部,则不创建新实例,而只是调用一下onNewIntent()。
“singleTask”模式系统会创建一个新的task然后在新的task底部创建一个Activity。但是,如果在另一个task中已经存在一个该Activity的实例的话,则所有在该实例之后创建的Activity(非”singleInstance”模式)都会被finish(包括其他task),然后该实例会调用一下Activity的onNewIntent()方法。虽然这一点在以上文档中没有提到,但却是发生了。
“singleInstance”模式与”singleTask”相似,区别是新建的task,”singleTask”模式允许放入其他Activity实例,而该模式下只允许存在其本身。

除了在中声明该activity的加载模式,还可以在Intent中通过setFlags()方法设置以下三种加载方式,文档中是这么描述的:

FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().
This produces the same behavior as the “singleTask” launchMode value, discussed in the previous section.

FLAG_ACTIVITY_SINGLE_TOP
If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to onNewIntent(), instead of creating a new instance of the activity.
This produces the same behavior as the “singleTop” launchMode value, discussed in the previous section.

FLAG_ACTIVITY_CLEAR_TOP
If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).

其中FLAG_ACTIVITY_NEW_TASK相当于”singleTask”模式,FLAG_ACTIVITY_SINGLE_TOP相当于”singleTop”模式,而FLAG_ACTIVITY_CLEAR_TOP虽然和”singleTask”很相似,但还是有区别的,区别在于如果目标Activity是”standard”模式,则目标也会先被destroy然后才创建,且不会调用onNewIntent()方法。

文章作者:admin
本文地址:http://www.zmkm.info/archives/189
版权所有 © 转载时必须以链接形式注明作者和原始出处!

One Response to “Android中task介绍”

  1. #1 淘宝最好的减肥药 回复 | 引用 Post:2011-07-25 12:49

    一字不落地看完了,精华

发表评论