Defining launch modes

  • 1、Using the manifest file
    • "standard" (the default mode)
    • "singleTop"
    • "singleTask"
    • "singleInstance"
  • 2、Using Intent flags
    • FLAG_ACTIVITY_NEW_TASK
    • FLAG_ACTIVITY_SINGLE_TOP
    • FLAG_ACTIVITY_CLEAR_TOP

#Defining launch modes
Launch modes allow you to define how a new instance of an activity is associated with the current task. You can define different launch modes in two ways:

Using the manifest file
When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.

Using Intent flags
When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity should associate with the current task.

As such, if Activity A starts Activity B, Activity B can define in its manifest how it should associate with the current task (if at all) and Activity A can also request how Activity B should associate with current task. If both activities define how Activity B should associate with a task, then Activity A’s request (as defined in the intent) is honored over Activity B’s request (as defined in its manifest).1

Note: Some launch modes available for the manifest file are not available as flags for an intent and, likewise, some launch modes available as flags for an intent cannot be defined in the manifest.

1、Using the manifest file

When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the activity element’s launchMode attribute.

The launchMode attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode attribute:

“standard” (the default mode)

Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

“singleTop”

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).
For example, suppose a task’s back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default “standard” launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D’s launch mode is “singleTop”, the existing instance of D receives the intent through onNewIntent(), because it’s at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is “singleTop”.

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent().

“singleTask”

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.

“singleInstance”

Same as “singleTask”, except that the system doesn’t launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

As another example, the Android Browser app declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the element. This means that if your app issues an intent to open the Android Browser, its activity is not placed in the same task as your app. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Back button always takes the user to the previous activity. However, if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.2


Figure 4. A representation of how an activity with launch mode “singleTask” is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.

For more information about using launch modes in the manifest file, see the element documentation, where the launchMode attribute and the accepted values are discussed more.

Note: The behaviors that you specify for your activity with the launchMode attribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.3

2、Using Intent flags

When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity(). The flags you can use to modify the default behavior are:

FLAG_ACTIVITY_NEW_TASK

Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().
This produces the same behavior as the “singleTask” launchMode value, discussed in the previous section.

FLAG_ACTIVITY_SINGLE_TOP

If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to onNewIntent(), instead of creating a new instance of the activity.
This produces the same behavior as the “singleTop” launchMode value, discussed in the previous section.

FLAG_ACTIVITY_CLEAR_TOP

If the activity being started 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 are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).
There is no value for the launchMode attribute that produces this behavior.

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

Note: If the launch mode of the designated activity is “standard”, it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That’s because a new instance is always created for a new intent when the launch mode is “standard”.4

参考文档:https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes


  1. 当Activity A 启动Activity B时,如果Activity B在manifest中定义了launch mode 并​且 Activity A在启动Activity B时使用的Intent也设置了Intent flags,则​​​​​最终应当遵从Intent flags中设定的行为。 ↩︎

  2. 特别注意一下该行为。 ↩︎

  3. Activity使用launchMode指定的启动行为(在AndroidManifest.xml文件中使用标签指定launchMode)可被启动该Activity的Intent指定的启动行为(使用Intent flags指定)覆盖,即Intent flags 指定的启动行为优先于Manifest文件中指定的launchMode; ↩︎

  4. 使用 Intent(Intent flags 为FLAG_ACTIVITY_CLEAR_TOP) 启动Activity A(其launch mode是“standard”)时,则current stack中的Activity A的实例将会被移除,并启动一个新的实例来响应该Intent。因为当launch mode是“standard”时,总是创建一个新实例来响应 new intent. ↩︎

Understand Tasks and Back Stack--Defining launch modes相关推荐

  1. Android 任务栈空间,【Android】任务和返回栈(tasks and back stack)

    tasks and back stack 一个Task就是一组activity的集合.这些activity按照它们打开的顺序被放置于一个先进后出的栈中(back stack). 用户点击图标打开一个a ...

  2. Tasks and Back stack 详解

    原文地址:http://developer.android.com/guide/components/tasks-and-back-stack.html 一个应用往往包含很多activities.每个 ...

  3. activty在哪个栈里面_第二篇Activity:2、任务和返回堆栈(Tasks and Back Stack)之基本介绍...

    参考:http://developer.android.com/guide/components/tasks-and-back-stack.html 在Android中,一个应用程序里面,通常包含了多 ...

  4. Android笔记 activity生命周期 Tasks and back stack回退栈

    正常打开一个应用,先后调用Activity的onCreate.onStart.onResume 正常退出一个应用,先后调用Activity的onPause.onStop.onDestroy(进程被杀死 ...

  5. Android 之Tasks和Back Stack(任务和返回栈)

    2019独角兽企业重金招聘Python工程师标准>>> 介绍: 应用通常包含多个Activity.每个 Activity 均应围绕用户可以执行的特定操作设计,并且能够启动其他 Act ...

  6. Android Application Fundamentals——Android应用程序基础知识

    Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvid ...

  7. Android11 Acvitity启动流程1-ActivityStarter

    Android11 Acvitity启动流程1-ActivityStarter: 一.回顾 在学习Android app开发的时候,最先要学习的就是关于Activity的启动方式,Task和Back ...

  8. android 让dialog保持在最前_Android 面试进阶指南 —— 唠唠任务栈,返回栈和启动模式...

    Android 面试进阶指南目录 唠唠任务栈,返回栈和启动模式 唠唠 Activity 的生命周期 扒一扒 Context 为什么不能使用 Application Context 显示 Dialog? ...

  9. Activity精选内容,看看还有没有你的盲点!

    /   今日科技快讯   / 微软公司和谷歌近日表示,两家公司将暂停政治捐款,直到"评估上周(国会遭冲击)事件的影响之后". 微软公司在9日下达了此决定,微软表示:"政治 ...

最新文章

  1. VS2010解决方案不显示无法添加项目问题
  2. app包中的fragment和v4包中的fragment的使用的区别
  3. SAP Spartacus organization unit list的实现Component
  4. 数据结构之链式栈的一些基本操作
  5. python环绕文字_如何用css实现文字三面环绕图片?
  6. 分区表PARTITION table
  7. 面向对象的设计原则-类设计原则
  8. php蓝奏云解析源码,PHP获取蓝奏云直链解析源码
  9. crontab每小时运行一次(转)
  10. 刷脸即可解锁让iDevice取证不再难如登天
  11. openkore 207cn 中文加强版 2010.01.12【彪彪修改】
  12. 比较不错的MaciOS软件论坛
  13. orcale :SQL语句小测试select * from emp order by hiredate asc;
  14. [计算机组成原理] 考试前突击挂科训练
  15. 从零开始写一个Jison解析器(3/10):良好的开端是成功的一半——《政治学》 (亚里士多德)
  16. Linux开启root用户
  17. APICloud入门初体验
  18. Unity TouchScripts实例 - 判断在物体上滑动
  19. 操作系统实验八:页面置换模拟程序设计
  20. 免费回收站恢复软件有哪些?数据恢复软件,这三款就足够了

热门文章

  1. April Fools Contest 2017 题解
  2. 关于数据库隔离问题说明及解决
  3. python发人人状态
  4. 如何使用RichEdit
  5. JavaScript对Json的增删改属性
  6. SqlServer 执行计划及Sql查询优化初探
  7. ORACLE常用命令【转】
  8. 获取当前周、上一周、下一周日期
  9. 李爽久:平安云如何解决金融同业互联专线痛点
  10. BZOJ 4516 [Sdoi2016] 生成魔咒