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's
"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".
The
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.
The
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对于standard和singleTop模式,它们可以出现在任何的Task任务中。
4.是否实例化新的instance来处理Intent请求:
4.1对于standard模式,一个新的实例会被创建并响应每一个intent
4.2对于singleTop模式,如果目标任务中的栈顶存在该activity的实例,那么该栈顶的实例将用于处理这个新的intent.如果目标任务中的栈顶不存在该activity实例,一个新的activity将创建并PUSH到目标任务TASK的任务栈顶。
Example如下:
相关文章推荐
- Python多线程返回执行结果
- 类字面常量
- PHP判断设备
- Android应用开发中View绘制的一些优化点解析
- hdu1505 City Game--DP/栈
- JSP验证码
- javac
- java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/reflection/ReflectionProviderFactory
- iOS开发证书转移
- 文件浏览器及数码相框-1
- 集成融云拍照,图片选择报错问题解决方案。
- 升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错
- Sitemesh 3 的使用及配置
- maven 下载源码
- 如何学习?
- 使用runtime关联对象将视图添加到视图的类目里
- Dialog对话框
- Vmware vSphere 6.0之安装 vCenter Server Appliance
- Qt实现网络数据传输 HttpClient
- 工具类:在 PCH 文件中宏定义一个取色方法