Activity有四种启动模式:

1.standard(标准)    2.singleTop    3.singleTask  4.singleInstance

标识某个Activity的启动模式,有两种方式:

1.一种是通过AndroidManifest.xml    2.一种是通过Intent的标识

通过AndroidManifest.xml来标识:

  1. <activity android:name=".Activity1"
  2. android:launchMode="standard"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>

通过Intent的Flag来标识:

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_SINGLE_TOP

FLAG_ACTIVITY_CLEAR_TOP

------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_NEW_TASK---没有对应的启动模式,它的部分特征与singleTask类似。

public static final int FLAG_ACTIVITY_NEW_TASK

Added in API level 1

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 Tasks and Back Stack for more information about tasks.

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.

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 FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.

Constant Value: 268435456 (0x10000000)

使用这个特征的Intent,系统会为被启动的Activity创建一个新的task,然后被启动的Activity存放在回退栈的底部。

实验:

1.在同一个应用中:

在启动Acitivyt时,设定FLAG_ACTIVITY_NEW_TASK标识

Intent newTask = new Intent(MainActivity.this ,com.intentflag.activity.ActivityNewTaskFlag.class);

newTask.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity( newTask);

效果如图:

由上两个图,可以发现,它们的taskId都是一样的,这说明,使用FLAG_ACTIVITY_NEW_TASK标识时,

在同一个应用中,并没有上述描述的效果,会启动一个新的TASK来存放被启动的Activity实例。

从times值得变化来看,如果当前task中,有被启动Activity的实例,那么,即使使用了FLAG_ACTIVITY_NEW_TASK标识,系统依然会创建一个新的实例。

2.在两个不同的应用中:

1).在使用FLAG_ACTIVITY_NEW_TASK标识的情况下

这是被启动Activity所在的应用:

这是启动者所在的应用:

成功启动后:

然后,再按下返回键:

可以知道:1.被启动的Activity是与启动者处于不同的task中的。2.系统会创建一个新的被启动Activity实例。

3.新的Activity是处在一个新的task的顶部,如果该task已经存在的话,则该Activity实例,位于栈顶。

2).在不使用FLAG_ACTIVITY_NEW_TASK的情况下:

从结果可以知道,如果不使用FLAG_ACTIVITY_NEW_TASK,那么,被启动的Activity将会与启动者使用同一个task,是新创建一个Activity,然后,将该Activity实例存放到与启动者使用的task中。

总结,FLAG_ACTIVITY_NEW_TASK,适合,启动哪些与被启动者不是同一个应用的Activity。一个应用,用一个task来组织它的所有Activity。

--------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_SINGLE_TOP

public static final int FLAG_ACTIVITY_SINGLE_TOP

Added in API level 1

If set, the activity will not be launched if it is already running at the top of the history stack.

Constant Value: 536870912 (0x20000000)

使用这个标识去

例子,在两个应用之间来使用:

被启动Activity,在另外一个应用中是处于栈顶的:

然后,在另外一个应用中,使用FLAG_ACTIVITY_SINGLE_TOP标识来启动:

然后,启动之后的结果是:

经过实验发现,并没有上述宣称的效果。

可以知道,是创建了一个新的Cactivity。

---------------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_CLEAR_TOP

public static final int FLAG_ACTIVITY_CLEAR_TOP

Added in API level 1

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.

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.

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 FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with 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.

See Tasks and Back Stack for more information about tasks.

Constant Value: 67108864 (0x04000000)

例子:

在默认情况下,只使用了FLAG_ACTIVITY_CLEAR_TOP标识

根据文档的描述,A->B->C->D,要启动B这个Activity,然后,使用的标识是FLAG_ACTIVITY_CLEAR_TOP,

然后,回退栈中的其它Activity会被清除掉,并且,B也会被销毁掉,然后重新创建。

最后的结果是:A-->B(新创建的)

实验:

然后,按下返回键,结果是返回到A,这说明,B,C,D都被清除掉了,栈顶是新的B。

这个效果,与文档所说明的效果是相同的。

Intent flag 与启动模式的对应关系的更多相关文章

  1. (转)Intent flag 与启动模式的对应关系

    原文地址:http://www.cnblogs.com/ttylinux/p/4069513.html Activity有四种启动模式: 1.standard(标准)    2.singleTop   ...

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

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

  3. android启动模式2

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

  4. Activity启动模式 Tasks和Back Stack

    http://www.cnblogs.com/mengdd/archive/2013/06/13/3134380.html Task是用户在进行某项工作时需要与之交互的一系列activities的集合 ...

  5. Android的启动模式(下)

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

  6. Android进阶--Acticivity的启动模式

    一.引言 我们在多次启动同一个Activity时,系统默认会重复创建多个实例,这样看上去便十分的愚蠢,所以android在设计时提供了启动模式来修改系统的默认行为.目前有四种启动模式:standard ...

  7. Android:图解四种启动模式 及 实际应用场景解说

    在一个项目中会包括着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种“后进先出”的栈结构.举个栗子,若我们多次启动同一个Activity.系统会创建多个实例依次放入 ...

  8. Android之Activity系列总结(三)--Activity的四种启动模式

    一.返回栈简介 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈,也叫任务栈)中. 首先介绍一下任务栈: (1)程序打开时 ...

  9. Android_Activity的生命周期、跳转方式及参数传递、启动模式。

    Activity的生命周期: onCreat ,onStart,onResume,onPause,onRestart,onStop,onDestroy Activity之间的跳转分为显式跳转和隐式跳转 ...

随机推荐

  1. 最好的vim教程莫过于vimtutor

    最好的vim教程莫过于vimtutor 直接运行vimtutor即可

  2. java 类的静态变量

    主要是记录一个奇葩的现象,java类中的静态变量,不仅可以通过类名称直接调用,而且还可以通过类的实力对象调用,java是不存在静态类的,如果非要用静态的类那就是内部类. 类中的静态变量是存储在JVM方 ...

  3. JavaScript的闭包原理

    什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 个人的理解是 ...

  4. vs2013安装闪退及vs2010 vs2013打开时提示 未能完成的操作 及vs2013安装时出现图片后闪退

    vs2013打开时提示如上图,vs2010只有  未能完成的操作  这样的提示. 这时.net 4.0开发的程序打开也毫无反应,应该是.net framework出了问题.查看控制面板-卸载程序,发现 ...

  5. Runner站立会议08

    会议时间:2016.4.27  21.10~21.25 地点:基教负一层 今天:看日历的代码,网上下的,没有注释 明天:继续看代码 困难:代码看不懂 会议照片: 燃尽图:

  6. lombok+slf4j+logback SLF4J和Logback日志框架详解

    maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...

  7. Linux的io机制

    Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...

  8. Python之路【第七篇续】:进程、线程、协程

    Socket Server模块 SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端.即:每个客户端请求连接到服务器时 ...

  9. 清除inline-block元素间距

    方法1 .clear{ letter-spacing: -4px;/*根据不同字体字号或许需要做一定的调整*/ word-spacing: -4px; font-size: 0;} 方法2 .clea ...

  10. 浅谈T-SQL中的派生表和CTE

    引言 表表达式是一种命名的查询表达式,代表一个有效的关系表.MSSQL支持4种类型的表表达式,它们分别是:派生表.公用表表达式(CTE).视图以及内联表值函数.表表达式并不是物理上真实存在的对象,它们 ...