您的位置:首页 > 其它

Activity启动模式总结

2016-03-22 15:43 507 查看
FLAG_ACTIVITY_NEW_TASK


FLAG_ACTIVITY_CLEAR_TOP


FLAG_ACTIVITY_RESET_TASK_IF_NEEDED


FLAG_ACTIVITY_SINGLE_TOP




Theprincipal
<activity>
attributesare:

taskAffinity


launchMode


allowTaskReparenting


clearTaskOnLaunch


alwaysRetainTaskState


finishOnTaskLaunch


Launchmodes

Therearefourdifferentlaunchmodesthatcanbeassignedtoan<activity>element'slaunchModeattribute:

"standard"(thedefaultmode)

"singleTop"

"singleTask"

"singleInstance"

"
standard
"and"
singleTop
"
modes,

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

1.1.1Affinitiesandnewtasks

Bydefault,alltheactivitiesinanapplicationhaveanaffinityforeachother—thatis,there'sapreferenceforthemalltobelongtothesametask.However,anindividualaffinitycanbesetforeachactivitywith
thetaskAffinityattributeofthe<activity>element.Activitiesdefinedindifferentapplicationscanshareanaffinity,oractivitiesdefinedinthesameapplicationcanbeassigned
differentaffinities.Theaffinitycomesintoplayintwocircumstances:WhentheIntentobjectthatlaunchesanactivitycontainstheFLAG_ACTIVITY_NEW_TASKflag,andwhenanactivityhasitsallowTaskReparentingattribute
setto"true".

TheFLAG_ACTIVITY_NEW_TASKflag

Asdescribedearlier,anewactivityis,bydefault,launchedintothetaskoftheactivitythatcalledstartActivity().It'spushedontothesamestackasthecaller.However,iftheIntent
objectpassedtostartActivity()containstheFLAG_ACTIVITY_NEW_TASKflag,thesystemlooksforadifferenttasktohousethenewactivity.Often,asthenameoftheflagimplies,
it'sanewtask.However,itdoesn'thavetobe.Ifthere'salreadyanexistingtaskwiththesameaffinityasthenewactivity,theactivityislaunchedintothattask.Ifnot,itbeginsanewtask.

TheallowTaskReparentingattribute

IfanactivityhasitsallowTaskReparentingattributesetto"true",itcanmovefromthetaskitstartsintothetaskithasanaffinityfor
whenthattaskcomestothefore.Forexample,supposethatanactivitythatreportsweatherconditionsinselectedcitiesisdefinedaspartofatravelapplication.Ithasthesameaffinityasotheractivitiesinthesameapplication(thedefaultaffinity)
anditallowsreparenting.Oneofyouractivitiesstartstheweatherreporter,soitinitiallybelongstothesametaskasyouractivity.However,whenthetravelapplicationnextcomesforward,theweatherreporterwillbereassignedtoanddisplayedwiththat
task.

Incontrast,the"
singleTask
"and"
singleInstance
"
modesmarkactivitiesthatarealwaysattherootofatask.Theydefineatask;they'reneverlaunchedintoanothertask.


Activity启动模式总结

1Activity启动模式:

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



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

2Actitivy能够实例化的次数:

standard
singleTop模式,能实例化多次,能属于多个任务,一个任务也能拥有多个实例。

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

3activity能否在自己的task中存在其他的Task:

3.1对于singleInstance模式,当前的Task中只有这个activity的单一实例。

3.2对于singleTask模式,这种activity一定是Task的根activity,可以在本Task中启动其他的Activity。

3.3对于standardsingleTop模式,它们可以出现在任何的Task任务中。

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

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

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

Example如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: