Activity的四种启动模式和相关的Intent flag

2023-10-30

一、启动模式

1、 standard模式

        默认模式,可以不用写配置。在这个模式下,都会默认创建一个新的实例。因此,在这种模式下,可以有多个相同的实例,也允许多个相同Activity叠加。应用场景:绝大多数Activity。

2、singleTop模式

        栈顶复用模式,如果要开启的activity在任务栈的顶部已经存在,就不会创建新的实例,而是调用 onNewIntent() 方法来将 intent 转送给该实例,避免栈顶的activity被重复的创建。

  • singleTop模式,只在当前任务栈中生效。

  • 如果通过 startActivityForResult 启动一个设置了 singleTop 的 activity,singleTop 模式将无效。

3、singleTask模式

        栈内复用模式,activity只会在任务栈里面存在一个实例。如果要激活的activity,在任务栈里面已经存在,就不会创建新的activity,而是复用这个已经存在的activity,调用onNewIntent() 方法,并且清空这个activity任务栈上面所有的activity, 如果实例不存在,就创建实例并放入栈中。    

4、singleInstance模式

        与 "singleTask" 相似,唯一不同的是系统不会将任何其他 Activity 启动到包含该实例的任务中。该 Activity 始终是其任务唯一的成员;由该 Activity 启动的任何 Activity 都会在其他的任务中打开。

onNewIntent() 使用注意

方法体中需手动调用 setIntent(intent),否则之后的 getIntent() 获取的都是旧的 intent 对象。

二、常用的 Intent Flag

1、 FLAG_ACTIVITY_NEW_TASK

    /**
     * If set, this activity will become the start of a new task on this
     * history stack.  A task (from the activity that started it to the
     * next task activity) defines an atomic group of activities that the
     * user can move to.  Tasks can be moved to the foreground and background;
     * all of the activities inside of a particular task always remain in
     * the same order.  See
     * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
     * Stack</a> for more information about tasks.
     *
     * <p>This flag is generally used by activities that want
     * to present a "launcher" style behavior: they give the user a list of
     * separate things that can be done, which otherwise run completely
     * independently of the activity launching them.
     *
     * <p>When using this flag, if a task is already running for the activity
     * you are now starting, then a new activity will not be started; instead,
     * the current task will simply be brought to the front of the screen with
     * the state it was last in.  See {@link #FLAG_ACTIVITY_MULTIPLE_TASK} for a flag
     * to disable this behavior.
     *
     * <p>This flag can not be used when the caller is requesting a result from
     * the activity being launched.
     */
    public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;

     如果设置了,此活动将成为此历史堆栈上新任务的开始。当使用这个 flag 时,如果 task 中已经有了你要启动的 Activity ,就不再启动一个新的 Activity;当前 task 会被带到前台。可以用 FLAG_ACTIVITY_MULTIPLE_TASK 使这种行为无效。当调用者 startActivityForResult() 时,不能使用此标志。

     比如栈中情况是 A,B,C,在 C 中启动 D,如果在 Manifest.xml 文件中给 D 添加了 Affinity(默认是包名) 的值和 C 所在的 Task 中的不一样,则会在新标记的 Affinity 所存在的 Task 中看 D 是否已经启动,如果已经启动直接将 D 所在的 task 带入到前台,否则直接将 activity 启动;如果是默认的或者指定的 Affinity 和 Task 一样,就和标准模式一样启动一个新的 Activity。此 flag 与启动模式 singleTask 效果不太一样,对于非 Activity 启动的 Activity(比如Service或者通知中启动的Activity)需要显示的设置 Intent.FLAG_ACTIVITY_NEW_TASK。

2、 FLAG_ACTIVITY_CLEAR_TOP

    /**
     * If set, and the activity being launched is already running in the
     * current task, then instead of launching a new instance of that activity,
     * all of the other activities on top of it will be closed and this Intent
     * will be delivered to the (now on top) old activity as a new Intent.
     *
     * <p>For example, consider a task consisting of the activities: A, B, C, D.
     * If D calls startActivity() with an Intent that resolves to the component
     * of activity B, then C and D will be finished and B receive the given
     * Intent, resulting in the stack now being: A, B.
     *
     * <p>The currently running instance of activity B in the above example will
     * either receive the new intent you are starting here in its
     * onNewIntent() method, or be itself finished and restarted with the
     * new intent.  If it has declared its launch mode to be "multiple" (the
     * default) and you have not set {@link #FLAG_ACTIVITY_SINGLE_TOP} in
     * the same intent, then it will be finished and re-created; for all other
     * launch modes or if {@link #FLAG_ACTIVITY_SINGLE_TOP} is set then this
     * Intent will be delivered to the current instance's onNewIntent().
     *
     * <p>This launch mode can also be used to good effect in conjunction with
     * {@link #FLAG_ACTIVITY_NEW_TASK}: if used to start the root activity
     * of a task, it will bring any currently running instance of that task
     * to the foreground, and then clear it to its root state.  This is
     * especially useful, for example, when launching an activity from the
     * notification manager.
     *
     * <p>See
     * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
     * Stack</a> for more information about tasks.
     */
    public static final int FLAG_ACTIVITY_CLEAR_TOP = 0x04000000;

     如果申明了 launch mode 是 "multiple" (默认情况就是)且没有在同一个 Intent 中设置 FLAG_ACTIVITY_SINGLE_TOP,Activity 将会结束且重新创建(回调 onCreate 生命周期方法);对于其他所有的 launch modes 或者设置了 FLAG_ACTIVITY_SINGLE_TOP,Intent 将被分发给实例的 onNewIntent() 方法。

     比如栈中情况是 A,B,C,D,在 D 中启动 B(加入该flag), 栈中的情况将为 A,B,B 会执行 onCreate() ...。如果希望与 launch mode 中 singleTask 效果相同执行 onNewIntent(),可以同时加上 FLAG_ACTIVITY_SINGLE_TOP。  

3、 FLAG_ACTIVITY_SINGLE_TOP 

    /**
     * If set, the activity will not be launched if it is already running
     * at the top of the history stack.
     */
    public static final int FLAG_ACTIVITY_SINGLE_TOP = 0x20000000;

     如果设置,如果此 activity 已经在历史堆栈的顶部将不会被启动。

     相当于 launch mode 的 singleTop,比如栈中情况是 A,B,C,D,在 D 中启动D(加入该flag),栈中的情况还是 A,B,C,D。

4、 FLAG_ACTIVITY_CLEAR_TASK

    /**
     * If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
     * this flag will cause any existing task that would be associated with the
     * activity to be cleared before the activity is started.  That is, the activity
     * becomes the new root of an otherwise empty task, and any old activities
     * are finished.  This can only be used in conjunction with {@link #FLAG_ACTIVITY_NEW_TASK}.
     */
    public static final int FLAG_ACTIVITY_CLEAR_TASK = 0X00008000;

     如果在一个 Intent 中设置,会导致在此 activity 开启之前,任何与该 activity 相关的 task 都会被清除。此 activity 将会是一个空 task 的最底部的 activity,之前所有的 activities 将被结束,此 flag 只能与 FLAG_ACTIVITY_NEW_TASK 配合使用。

5、 FLAG_ACTIVITY_REORDER_TO_FRONT

    /**
     * If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
     * this flag will cause the launched activity to be brought to the front of its
     * task's history stack if it is already running.
     *
     * <p>For example, consider a task consisting of four activities: A, B, C, D.
     * If D calls startActivity() with an Intent that resolves to the component
     * of activity B, then B will be brought to the front of the history stack,
     * with this resulting order:  A, C, D, B.
     *
     * This flag will be ignored if {@link #FLAG_ACTIVITY_CLEAR_TOP} is also
     * specified.
     */
    public static final int FLAG_ACTIVITY_REORDER_TO_FRONT = 0X00020000;

     如果在 Intent 中设置 ,如果待启动的 activity 已经开启在运行了,此 flag 将使其位于任务历史堆栈的前面。例如栈中情况是 A,B,C,D,如果 D 启动 B,栈中将变成A,C,D,B (B 将回调 onNewIntent() )。如果 FLAG_ACTIVITY_CLEAR_TOP 也被指定,此标志将被忽略。       

6、 FLAG_ACTIVITY_FORWARD_RESULT  

    /**
     * If set and this intent is being used to launch a new activity from an
     * existing one, then the reply target of the existing activity will be
     * transfered to the new activity.  This way the new activity can call
     * {@link android.app.Activity#setResult} and have that result sent back to
     * the reply target of the original activity.
     */
    public static final int FLAG_ACTIVITY_FORWARD_RESULT = 0x02000000;

     如果在 Intent 中设置此 flag 从现有的 activity 去开启一个新的 activity ,现有的 activty 将会把回复的目标转移给新 activity. 新 activity 可以调用 setResult() 将结果发送给现有 activity 的回复目标。

     例如:A 通过 startActivityForResult 启动 B,B 启动 C,但 B 为过渡页可以 finish 了,A 在期望 C 把结果返回。这种情况,B 可以在启动 C 的时候加入该flag。 

7、 FLAG_ACTIVITY_PREVIOUS_IS_TOP

    /**
     * If set and this intent is being used to launch a new activity from an
     * existing one, the current activity will not be counted as the top
     * activity for deciding whether the new intent should be delivered to
     * the top instead of starting a new one.  The previous activity will
     * be used as the top, with the assumption being that the current activity
     * will finish itself immediately.
     */
    public static final int FLAG_ACTIVITY_PREVIOUS_IS_TOP = 0x01000000;

     如果当前的 activity 在开启新 activity 的 intent 设置此 flag, 当前 activity 之前的 activity 将被当视为 top,当前的 activity 将立即结束。

     例如:栈中情况 A,B,C,C 启动 D 时使用此标志,在启动时 C 不会被当成栈顶 Activity,而是 B 作为栈顶启动 D,然后 C 会 finish()。经常与 FLAG_ACTIVITY_FORWARD_RESULT 一起配合使用。

8、 FLAG_ACTIVITY_NO_HISTORY

    /**
     * If set, the new activity is not kept in the history stack.  As soon as
     * the user navigates away from it, the activity is finished.  This may also
     * be set with the {@link android.R.styleable#AndroidManifestActivity_noHistory
     * noHistory} attribute.
     *
     * <p>If set, {@link android.app.Activity#onActivityResult onActivityResult()}
     * is never invoked when the current activity starts a new activity which
     * sets a result and finishes.
     */
    public static final int FLAG_ACTIVITY_NO_HISTORY = 0x40000000;

     如果设置了此 flag,开启的 activity 将不会存在历史堆栈中。一旦用户离开它,activity 就结束了。也可以用{@link android.R来设置。styleable # AndroidManifestActivity_noHistory noHistory}属性。如果设置了此 flag,activity 将不会回调 onActivityResult()。

9、 FLAG_ACTIVITY_TASK_ON_HOME

    /**
     * If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
     * this flag will cause a newly launching task to be placed on top of the current
     * home activity task (if there is one).  That is, pressing back from the task
     * will always return the user to home even if that was not the last activity they
     * saw.   This can only be used in conjunction with {@link #FLAG_ACTIVITY_NEW_TASK}.
     */
    public static final int FLAG_ACTIVITY_TASK_ON_HOME = 0X00004000;

     如果 intent 中设置此 flag 将使新启动的 task 置于当前 home activity 任务之上(如果有的话)。也就是说,从任务中返回总是会将用户返回到home,即使这不是他们看到的最后一个 activity。此 flag 只能与 FLAG_ACTIVITY_NEW_TASK 配合使用。

10、FLAG_EXCLUDE_STOPPED_PACKAGES   

* If set, this intent will not match any components in packages that
* are currently stopped.  If this is not set, then the default behavior
* is to include such applications in the result.

     如果设置,intent 将与当前停止的包中的任何组件不匹配。如果未设置此flag,则默认行为是在结果中包含此类应用程序。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Activity的四种启动模式和相关的Intent flag 的相关文章

  • java:为什么主线程等待子线程完成

    我有一个简单的java程序 主线程 main 创建并启动另一个线程t class T extends Thread Override public void run while true System out println Inside
  • 如何解决Android错误类型3?

    下面是我在项目中使用的清单格式 但是每当我尝试运行模拟器时 我都会收到下面提到的错误 请给我一个解决该错误的准确解决方案 工具信息 Android studio Nexus S API 22 android 5 1 1 我的清单文件格式
  • 如何检查单词是否在wordNet中

    我开始了解wordNet直到我知道我找到了synonymous对于一个特定的词 现在我有一个文件 我想使用标记化该文本n gram例如 String s I like to wear tee shirt 使用后n gram这将是 I lik
  • 在 Eclipse 中删除空块之前的新行

    我更喜欢奥尔曼式 http en wikipedia org wiki Brace style Allman style大括号 例如 if foo magical prancing unicorn stuff 而不是 if foo unma
  • 生成一定长度的所有排列

    假设我们有一个字母表 abcdefghiklimnop 如何以有效的方式以五个一组的形式重复该字母表来递归生成排列 几天来我一直在为此苦苦挣扎 任何反馈都会有帮助 本质上这与 生成给定字符串的所有排列 https stackoverflow
  • java绕中心旋转矩形

    我想围绕其中心点旋转一个矩形 它应该保留在应该绘制的位置并在该空间中旋转 这是我的代码 AffineTransform transform new AffineTransform transform rotate Math toRadian
  • 我怎样才能实现CoverFlow视图[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想用点线布局实现溢出视图 目前我正在使用 polidea 封面流库 URL github https
  • BigDecimal汇总统计

    我有一个 BigDecimal 列表 List
  • Android 导航 DeepLinks - 如何区分使用导航操作进行导航与深层链接

    我有导航操作和深层链接 根据您导航的位置 将您带到某个片段 我想确定是使用了深层链接还是仅使用了导航操作 这正是KEY DEEP LINK INTENT https developer android com reference andro
  • 在 Kotlin 中声明静态属性?

    My Java code public class Common public static ModelPengguna currentModelPengguna public class Common companion object v
  • 在 Spring MVC 中将请求写入文件

    我希望能够将整个请求写入 Spring MVC 控制器中的文件 我已尝试以下操作 但即使我使用大量参数发出 POST 请求 文件也始终为空 RequestMapping method RequestMethod POST value pay
  • Desire HD 中的应用程序崩溃

    由于某些莫名其妙的原因 我的应用程序在 HTC Desire HD 上崩溃了 它在其他手机和模拟器中运行良好 这是崩溃报告 java lang RuntimeException Unable to start activity Compon
  • 用于生成 ISO 文件的 Maven 插件

    有没有可以生成ISO镜像的maven插件 我需要获取一些模块的输出 主要是包含 jar 的 zip 文件 并将它们组合成一个 ISO 映像 Thanks 现在有一个 ISO9660 maven 插件可以完成这项工作 https github
  • android中如何将字符串转换为unicode

    我正在解析一些unicodes from json to my android应用程序 API 给出unicodes像这样的图标 ue600 当我将这个unicode直接添加到textview like textview setText u
  • 亚马逊 Linux - 安装 openjdk-debuginfo?

    我试图使用jstack在 ec2 实例上amazon linux 所以我安装了openjdk devel包裹 sudo yum install java 1 7 0 openjdk devel x86 64 但是 jstack 引发了异常j
  • 如何检查设备上是否安装了电子邮件客户端

    我需要检查设备上是否安装了电子邮件客户端 我使用了以下代码 但它对我不起作用 public boolean isIntentAvailable final PackageManager packageManager getApplicati
  • FragmentMap + ActionBar 选项卡

    我一直在尝试插入一个MapView进入一个ActionBar Tab 但我什至无法解决问题 即使谷歌搜索 这是主要活动 Override public void onCreate Bundle savedInstanceState supe
  • Android 可扩展列表视图随机播放子项

    你好 我正在使用 Android Expandable listview 并用不同的视图在其中膨胀子视图 我遇到的问题是 当我展开视图然后打开另一个父视图时 布局中的子视图会变得混乱并在代码中膨胀错误的布局 这是我的两个项目的示例代码 这是
  • MyBatis 枚举的使用

    我知道以前有人问过这个问题 但我无法根据迄今为止找到的信息实施解决方案 所以也许有人可以向我解释一下 我有一个表 状态 它有两列 id 和 name id是PK 我不想使用 POJO Status 而是使用枚举 我创建了这样一个枚举 如下所
  • Android:通过查找带有标签而不是 ID 的容器来添加片段

    我正在使用 for 循环创建片段的多个实例 在每个片段中 我需要添加另一组子片段 为此 我需要找到正确的容器 如果我使用容器的 ID 所有子片段都会添加到第一个父片段 而不是它们自己的父片段 在我的主要片段中 for ParentFragm

随机推荐

  • 机器学习综述论文笔记:Machine Learning: A Review of Learning Types

    机器学习review Paper Machine Learning A Review of Learning Types 这是一篇关于机器学习的综述 里面简述了各种现有的机器学习技术 1 主要的方法 监督 无监督 强化 1 1 监督学习 数
  • Redis原理篇(二)网络模型

    一 用户空间和内核空间 应用需要通过Linux内核与硬件交互 内核本质也是应用 运行的时候也需要CPU资源 内存资源 用户应用也在消耗这些资源 为了避免用户应用导致冲突甚至内核崩溃 用户应用与内核是分离的 进程的寻址空间会划分为两部分 内核
  • 公司前端vue是用vscode开发工具写的,个人喜欢用idea,但是idea在保存代码的时候会自动去除代码行最后的空格,造成不该修改的地方修改了,影响代码提交

    取消这个功能 File Settings Editor General On Save Remave trailing
  • springBoot 观察者模式

    观察者设计模式 jie神说用订阅和发布来理解更好 我想了一下是的 为什么呢 因为监听器这个名词听起来是一个主动的 可实际监听器是一个被动的玩意 比如我们事件源发布一个事件 然后监听器订阅了这个事件就能做出动作 里面涉及到三个对象 事件源 事
  • 【2023】JAVA和PLC实现通讯读取写入数据,以三菱PLC举例

    1 创建maven工程引入依赖
  • --- Error: User Command terminated, Exit-Code = 1解决办法

    使用keil MDK编译项目时 compiling编译通过 但是文件最后出现错误 Error User Command terminated Exit Code 1 经查阅资料 MDK需要fromelf exe文件生成 bin 那么在重新安
  • uboot编译报错解决

    uboot编译报错 root ubuntu home gjt uboot u boot 2015 01 make scripts kconfig conf silentoldconfig Kconfig scripts kconfig co
  • Protobuf 使用(c++)

    一 Protobuf 安装 安装protobuf tar xvf protobuf cd protobuf autogen sh生成configure configure prefix usr local protobuf make mak
  • 用P5.js实现一个动态的绘画系统

    摘要 通过一段时间的学习 我发现码绘的可能性比我想象的要更大 我们可以用码绘实现很多手绘很难达到的效果 比如创作一幅会动的 能进行交互的画作 如何通过类似画笔的东西在屏幕上创作出时刻在改变的 并且我们可以进行实时修改的像动画一样的作品 这就
  • AutoSchedule和AutoTVM

    简介 AutoTVM 用户自己手写一个模版 在模版里面自己定义一下tune的参数 例如tile size等 给定一个模版 在这个模版里面去搜索参数 使得可以达到一组最好的参数使得张量计算的结果最好 但是 它是一种基于模板的方法 因此仍然需要
  • 构建一个Flex程序

    构建一个Flex程序 Flex定义了一个基于组件的开发模型 从而我们可以用来构建我们的程序 为了高效的设计与构建我们的程序 我们应该熟悉这个模型 以及程序开发步骤与布署过程 在这一章描述了我们用来创建一个程序的开发过程 在这一章所包含的如下
  • Neo4j的安装和简单使用

    1 首先是Neo4j的下载和安装 下载地址 https neo4j com download 我下载的是Community Edition 下载完毕 因为是 exe文件 直接双击安装即可 没有什么需要注意的 2 安装完毕 在第一次使用Neo
  • SAP ABAP 粘贴板负号前置

    场景 用户一般Ctrl C复制ALV数据到Excel处理 如果有负数的数值 负号在数值的后面 Excel不认识 要费劲巴拉的一个个改正 苦不堪言 本程序功能 在ALV复制数据后 直接更改剪贴板里面的数据 把负号提到前面 然后直接在Excel
  • 立创商城中元器件封装的3d模型导出STEP格式文件

    1 首先安装FreeCAD软件和注册立创账号 2 进入立创EDA专业版 同时登录立创EDA账号 立创EDA专业版网址 https pro lceda cn editor 3 新建一个工程并打开 4 把立创商城的商品编号复制到下面的元器件库中
  • Python 实现的关键词查找小工具

    引言 平时工作时 有时会遇到这样的情景 在一个目录及其子目录下所有的文本文件中查找某个关键字 词或者完整的句子 当然 如果是在Linux平台上 find egrep就能实现这样的功能 不过最近学习了Python tkinter相关的知识 自
  • 浏览器渲染原理 - 输入url 回车后发生了什么

    目录 渲染时间点 渲染流水线 1 解析 parse HTML 1 1 DOM树 1 2 CSSOM树 1 3 解析时遇到 css 是怎么做的 1 4 解析时遇到 js 是怎么做的 2 样式计算 Recalculate style 3 布局
  • SPSS——问卷分析(简要操作板,自己整理的有用信息)

    资料来源 SPSS统计应用实务 问卷分析与应用统计 一 复选题及其他方式的数据编码 复选题 对每一个选项分别编码 0 表示没有选此项答案 1 表示选了此项答案 Eg 一 您认为目前教改阻力来自哪些单位 可复选 1 教育行政机关 2 民意代表
  • 不得不服!Python速度虽然慢,但是它工作效率很高!

    写在前面 让我们来讨论一个我最近一直在思考的问题 Python 的性能 顺便说一下 我是 Python 的忠实拥趸 我在各种情况下都会积极尝试使用 Python 来解决问题 大家对 Python 最大的抱怨就是它的速度慢 有些人甚至因为 P
  • ORACL 11gPLSQL创建新用户无法以Normal登录得解决方法

    第一步 首先以Sysdba得身份登录Oracle数据库 第二步 先创建一表空间DATA 初始大小50MB 允许自动增长数据库文件 每次增长10MB 输入的SQL语句如下 E app 23595 oradata databasename DA
  • Activity的四种启动模式和相关的Intent flag

    一 启动模式 1 standard模式 默认模式 可以不用写配置 在这个模式下 都会默认创建一个新的实例 因此 在这种模式下 可以有多个相同的实例 也允许多个相同Activity叠加 应用场景 绝大多数Activity 2 singleTo