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.

Android中Activity启动模式探索的更多相关文章

  1. Android中Activity启动模式详解

    在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...

  2. 第二课android中activity启动模式

    一.标准启动模式可以用函数gettaskid得到任务的idtostring得到地址用textallcaps来设置是否全部大写应用启动自己是在任务栈里创建不同实例可以用返回来返回上一个任务栈在andro ...

  3. Android中的启动模式(下)

    在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上 ...

  4. Android开发——Activity启动模式详解

    1. Activity的启动模式 本文原创,转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52054893 1.1 Standard标 ...

  5. Android 之Activity启动模式(二)之 Intent的Flag属性

    首页博客链接关于我留言板 前面介绍了通过launchMode设置Activity的启动模式.本章接着介绍Activity的启动模式相关内容,讲解的内容是Intent与启动模式相关的Flag,以及and ...

  6. Android之Activity启动模式

    正常模式 每个应用都有一个任务栈,任务栈中保存着已创建的Activity,先创建的Activity先入栈,栈顶是当前正在显示的activity(running),这是正常模式下的Activity的管理 ...

  7. Android中Activity启动过程探究

    首先追溯到Activity的启动,随便启动一个自己写的demo项目,使用DDMS进行debug标记,然后在Debug中把主线程暂停,可以看到调用栈.如下图所示: 于是我们先看android.app.A ...

  8. android入门 — Activity启动模式

    1.standard模式 standard模式是系统的默认启动方式,每次激活Activity都会创建Activity,并放在任务栈中. 系统不会在乎活动是否已经存在于返回栈中,每次启动都会创建该活动的 ...

  9. Android中Activity的启动模式

    简介 Android中的活动启动方式分为4种:standard, singleTop, singleTask, singleInstance.可以在AndroidManifest.xml中通过给< ...

随机推荐

  1. HDU - 5963 朋友(思维题)

    题干 B君在围观一群男生和一群女生玩游戏,具体来说游戏是这样的: 给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根.接下来从女生开始,双 ...

  2. git clone 别人的项目的步骤

    1.)从github上克隆出来 git clone +项目地址 2)切换到你需要的分支 git checkout +分支名称 3)下载到你的电脑上之后,在项目根目录中 npm install 安装所有 ...

  3. ssh安全远程管理

    1.什么是ssh SSH 为 Secure Shell 的缩写,SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用 SSH 协议可以有效 ...

  4. java IO流 (二) IO流概述

    1.流的分类* 1.操作数据单位:字节流.字符流* 2.数据的流向:输入流.输出流* 3.流的角色:节点流.处理流 图示: 2.流的体系结构 说明:红框对应的是IO流中的4个抽象基类.蓝框的流需要大家 ...

  5. JavaScript动画实例:螺旋线

    数学中有各式各样富含诗意的曲线,螺旋线就是其中比较特别的一类.螺旋线这个名词来源于希腊文,它的原意是“旋卷”或“缠卷”.例如,平面螺旋便是以一个固定点开始向外逐圈旋绕而形成的曲线.在2000多年以前, ...

  6. 老司机带你玩转面试(3):Redis 高可用之主从模式

    前文回顾 建议前面文章没看过的同学先看下前面的文章: 「老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化」 「老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩.击穿. ...

  7. [译]使用DOT语言和GraphvizOnline来可视化你的ASP.NETCore3.0终结点01

    这是系列文章中的第一篇:使用GraphvizOnline可视化ASP.NETCore3.0终结点.. 第1部分-使用DOT语言来可视化你的ASP.NETCore3.0终结点(本文) 第2部分-向ASP ...

  8. 【测试工具】这些APP实用测试工具,不知道你就out了!

    本期,我将给大家介绍14款实用的测试工具,希望能够帮到大家!(建议收藏) UI自动化测试工具 1. uiautomator2 Github地址:https://github.com/openatx/u ...

  9. Python Hacking Tools - Web Scraper

    Preparation: Python Libray in the following programming: 1. Requests Document: https://2.python-requ ...

  10. EF中延迟加载的那些事

    延迟加载又称懒加载,通俗一点就是关联了一个对象,不用的时候不去查这个对象,当调用的时候再组织sql去查出这个对象的相关内容. 一.在使用EF时,我们会发现借助于框架生成的实体类中的的导航属性通常是标记 ...