Intent flag 与启动模式的对应关系

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。

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

时间: 2025-01-08 13:44:24

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

关于Android Activity启动的flag和启动模式

最近因为项目需求当中,需要按一个按键,退回到指定的Activity,有可能是连续退几个Activity,第一时间想到的是多写几个finish()......然后想想就不可能,查了下Activity的相关资料,假如需要回退到Activity A,那么把A的启动模式设置为singleTask就可以了,在Manifest.xml中A的声明里面加上一句话,Android::launchmode ="singleTask",当然这样会导致每次启动A的时候都会清除掉Activity栈中A上面的所有

Android中的启动模式(下)

在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上).好了,言归正传,在上一篇已经介绍过,activity在栈中默认不能重排,因此,应用中的一个activity可能被多次实例化并且压入同一个栈中,如图所示: 如果此时使用back键返回,activity的每个实例都将会按照打开的顺序重新出现.这势必会导致用户生体验效果,因此要改变这种现象或者解决上篇

Activity启动模式 Tasks和Back Stack

http://www.cnblogs.com/mengdd/archive/2013/06/13/3134380.html Task是用户在进行某项工作时需要与之交互的一系列activities的集合.这些activities按照它们被打开的顺序,被安放在一个堆栈里(back stack). 一个activity甚至可以打开其他应用的activity. 比如你的应用需要发送一个电子邮件,你可以定义一个intent来执行发送动作,intent包含一些必要的数据,然后启动另一个应用中的activit

android启动模式2

Android中的启动模式(下) 在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上).好了,言归正传,在上一篇已经介绍过,activity在栈中默认不能重排,因此,应用中的一个activity可能被多次实例化并且压入同一个栈中,如图所示: 如果此时使用back键返回,activity的每个实例都将会按照打开的顺序重新出现.这势必会导致用户生体验效

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

在一个项目中会包括着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种"后进先出"的栈结构.举个栗子,若我们多次启动同一个Activity.系统会创建多个实例依次放入任务栈中.当按back键返回时,每按一次,一个Activity出栈,直到栈空为止.当栈中无不论什么Activity.系统就会回收此任务栈. 上面这个样例中的Activity并没有设置启动模式,你会发现多次启动同一个Activity.而系统却创建了多个实例,白白浪费内存,这样的情况Andro

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

一.返回栈简介 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈,也叫任务栈)中. 首先介绍一下任务栈: (1)程序打开时就创建了一个任务栈, 用于存储当前程序的activity,所有的activity属于一个任务栈. (2)一个任务栈包含了一个activity的集合, 去有序的选择哪一个activity和用户进行交互:只有在任务栈栈顶的activity才可以跟用户进行交互. (3)任务栈可以移动到后台, 并且保留了每

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

在一个项目中会包含着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种"后进先出"的栈结构.举个栗子,若我们多次启动同一个Activity,系统会创建多个实例依次放入任务栈中,当按back键返回时,每按一次,一个Activity出栈,直到栈空为止,当栈中无任何Activity,系统就会回收此任务栈. 上面这个例子中的Activity并没有设置启动模式,你会发现多次启动同一个Activity,而系统却创建了多个实例,白白浪费内存,这种情况Android早

Android进阶--Acticivity的启动模式

一.引言 我们在多次启动同一个Activity时,系统默认会重复创建多个实例,这样看上去便十分的愚蠢,所以android在设计时提供了启动模式来修改系统的默认行为.目前有四种启动模式:standard.singleTop.singleTask和singleInstance. 二.如何设置启动模式 1.我们可以通过AndroidMenifest为Activity指定启动模式 <activity android:name=".SecondActivity" android:confi

任务栈 Activity的启动模式 Intent中的Flag taskAffinity

关于任务栈Task 栈的概念 栈(Stack)是一种常用的数据结构,栈只允许访问栈顶的元素,栈就像一个杯子,每次都只能取杯子顶上的东西,而对于栈就只能每次访问它的栈顶元素,从而可以达到保护栈顶元素以下的其他元素."先进后出"或"后进先出"就是栈的一大特点,先进入栈的元素总是要等到后进入栈的元素出栈以后才能出栈.递归就是利用到了系统栈,暂时保存临时结果,对临时结果进行保护. 栈的基本操作:压栈.弹栈 任务栈 Task简单的就是一组以栈的模式聚集在一起的Activity