Android中启动模式(launchMode)分为standard, singleTop, singleTask, singleInstance四种,可通过AndroidManifest.xml文件设置某个activity的启动模式。接下来会一一探索启动模式对app行为的影响。用到的app和activity构成图如下,每次改变launchMode都只针对App2的SecondActivity:

Standard

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:

  2. App1跳转到App2
    保持App2当前状态切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:

可以看出,当采用standard作为启动模式时(standard也是Activity的缺省启动模式),每次启动该Activity都会产生一个新的实例,App内的Activity处于同一任务栈中;不同App间Activity调起时,被调起者与调起者也处于同一任务栈,当然,ApplicationContext是不一样的。

singleTop

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:

  2. App1跳转到App2
    保持App2最顶端为SecondActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:

  3. 暂时修改App2:Second的跳转逻辑为Second->Second,即自己跳自己,log记录如下:

可以看出,当采用singleTop作为启动模式时,行为特性和standard基本一致,唯一不同的情形是,singleTop的Activity位于后退栈顶、且该任务栈正与用户交互时,当有新的Intent要打开这个Activity不再产生一个新的实例,而是使用调栈实例同时回调栈顶实例的onNewIntent()方法将新的Intent传入。

顺便附上doc说明:
If, when starting the activity, there is already an instance of the same activity class in the foreground that is interacting with the user, then re-use that instance. This existing instance will receive a call to {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with the new Intent that is being started.

singleTask

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:

  2. App1跳转到App2
    保持App2最顶端为ThirdActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:

可以看出,当采用singleTask作为启动模式时,行为特性和singleTop基本一致,唯一不同的情形是,使用旧Activity实例的情况不再局限于“该位于后退栈顶、且该任务栈正与用户交互时”,而是只要某个Task中已存在该Activity实例,就会调起该后台运行Task,意味着调起方Task与被调起方Task并不处于同一后退堆栈

顺便附上doc说明:
If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call to {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with the new Intent that is being started, and with the {@link android.content.Intent#FLAG_ACTIVITY_BROUGHT_TO_FRONT Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT} flag set. This is a superset of the singleTop mode, where if there is already an instance of the activity being started at the top of the stack, it will receive the Intent as described there (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). See the Tasks and Back Stack document for more details about tasks.

singleInstance

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:

    可以看到,页面扭转时SecondActivity独自拥有了一个任务栈,跳到Third后Third的任务栈和Main维持一致,所以这里,我们的App2内就存在着2个任务栈。猜想一下,这个时候我们的回退栈是什么样的?出乎意料,后退时,会先以任务栈为单位后退,当前一个任务栈清空后,再进入后一个任务栈的顶端逐一移除

  2. App1跳转到App2
    保持App2最顶端为ThirdActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:

可以看出,当采用singleInstance作为启动模式时,行为特性最特殊,被singleInstance标记的Activity会单独占有一个任务,如果已有一个Activity实例存在则不会再实例化新实例,而是把原实例所在任务唤醒到前台并回调其onNewIntent方法。

顺便附上doc说明:
Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its {@link android.app.Activity#onNewIntent Activity.onNewIntent()} method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.

最新文章

  1. 访问其他程序的SheardPreferents
  2. vs2012 Nuget错误:“未能解析此远程名称api.nuget.org”
  3. 在项目中 background transiton 带来的"便利"与“坑”
  4. POJ 2409 Let it Bead(Polya定理)
  5. careercup-栈与队列 3.3
  6. Kernel Regression from Nando's Deep Learning lecture 5
  7. .net通用权限框架B/S (四)--DAL数据层以及数据接口
  8. BZOJ 1622: [Usaco2008 Open]Word Power 名字的能量
  9. dubbo负载均衡策略及对应源码分析
  10. 《Thinking in Java》学习笔记(六)
  11. python webdriver环境搭建
  12. C#:在匿名方法中捕获外部变量
  13. java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒)
  14. Oracle报错#“ORA-01791: 不是 SELECTed 表达式”解决方法
  15. WiFi-ESP8266入门http(1)-建立服务器,直接发送网
  16. P1886 P2216 单调队列模板
  17. angular清除select空格
  18. dynamic load jar and init spring
  19. 初识TPOT:一个基于Python的自动化机器学习开发工具
  20. Express 4.x Node.js的Web框架----《转载》

热门文章

  1. HBase2.0 meta信息丢失的修复方法
  2. 洛谷 P1640 SCOI2010 连续攻击游戏 并查集
  3. NW.js Mac App 签名公证流程
  4. CSS学习之选择器优先级与属性继承
  5. msyql事务的四种隔离级别
  6. scrapy 基础组件专题(九):scrapy-redis 源码分析
  7. JVM详解之:运行时常量池
  8. Static关键字的使用
  9. Go Pentester - HTTP CLIENTS(3)
  10. T133308 57级返校测试重测-T3-成绩单