Activity启动模式总结

来源:互联网 发布:wow7.0前夕插件mac 编辑:程序博客网 时间:2024/05/17 23:03

FLAG_ACTIVITY_NEW_TASK 
FLAG_ACTIVITY_CLEAR_TOP 
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 
FLAG_ACTIVITY_SINGLE_TOP



 

The principal <activity> attributesare:

taskAffinity 
launchMode 
allowTaskReparenting 
clearTaskOnLaunch 
alwaysRetainTaskState 
finishOnTaskLaunch

 

Launch modes

There are fourdifferent launch modes that can be assigned to an <activity> element's launchMode attribute:

"standard" (the default mode) 
"
singleTop
"
singleTask
"
singleInstance"

 

 

 

 "standard" and"singleTop" modes,

如果通过Intent调用 startActivity()启动。并且不包含  FLAG_ACTIVITY_NEW_TASK 标记。 那么会遵循下面的启动方式:

1.1.1  Affinities and new tasks

Bydefault, all the activities in an application have an affinity foreach other — that is, there's a preference for them all to belong to the sametask. However, an individual affinity can be set for each activity with the taskAffinity attribute of the <activity> element. Activitiesdefined in different applications can share an affinity, or activities definedin the same application can be assigned different affinities. The affinitycomes into play in two circumstances: When the Intent object that launches anactivity contains the FLAG_ACTIVITY_NEW_TASK flag, and when anactivity has its allowTaskReparenting attribute set to "true".

The FLAG_ACTIVITY_NEW_TASK flag

Asdescribed earlier, a new activity is, by default, launched into the task of theactivity that called startActivity(). It's pushed onto the same stack as thecaller. However, if the Intent object passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looksfor a different task to house the new activity. Often, as the name of the flagimplies, it's a new task. However, it doesn't have to be. If there's already anexisting task with the same affinity as the new activity, the activity islaunched into that task. If not, it begins a new task.

The allowTaskReparenting attribute

If anactivity has its allowTaskReparenting attribute set to "true", it can move from thetask it starts in to the task it has an affinity for when that task comes tothe fore. For example, suppose that an activity that reports weather conditionsin selected cities is defined as part of a travel application. It has the sameaffinity as other activities in the same application (the default affinity) andit allows reparenting. One of your activities starts the weather reporter, soit initially belongs to the same task as your activity. However, when thetravel application next comes forward, the weather reporter will be reassignedto and displayed with that task.

 

 

Incontrast, the "singleTask" and "singleInstance" modes mark activities that arealways at the root of a task. They define a task; they're never launched intoanother task.

 

Activity启动模式总结

 

1 Activity启动模式:

1.1 standard 和 singleTop 模式启动方式如下。


1.2 相反,the "singleTask" and"singleInstance" modes 模式通常是task的根任务。它们定义一个Task,从来不会启动另外一个Task.

2 Actitivy能够实例化的次数:
 standard
singleTop 模式, 能实例化多次,能属于多个任务,一个任务也能拥有多个实例 。

"singleTask"and "singleInstance"  模式,只能有一个实例。

3 activity 能否在自己的task中存在其他的Task
3.1 对于 singleInstance 模式,当前的Task中只有这个activity的单一实例。

3.2 对于singleTask模式,这种activity一定是 Task的根activity ,可以在本Task中启动其他的Activity。
3.3 对于 standard singleTop 模式,它们可以出现在任何的Task任务中。

 

4. 是否实例化新的instance来处理Intent请求:

4.1 对于 standard模式, 一个新的实例会被创建并响应每一个intent

4.2 对于singleTop模式,如果目标任务中的栈顶存在该activity的实例,那么该栈顶的实例将用于处理这个新的intent . 如果目标任务中的栈顶不存在该activity实例,一个新的activity将创建并PUSH到目标任务TASK的任务栈顶。

Example 如下:

 

 

 

 






0 0
原创粉丝点击