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

时间:2021-12-19 18:04:14

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.在同一个应用中:

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

在启动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);

效果如图:

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

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

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

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

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

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

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

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

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

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

这是启动者所在的应用:

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

成功启动后:

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

然后,再按下返回键:

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

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

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

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

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

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

从结果可以知道,如果不使用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,在另外一个应用中是处于栈顶的:

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

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

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

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

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

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

可以知道,是创建了一个新的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(新创建的)

实验:

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

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

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

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

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

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

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

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