Android android:documentLaunchMode属性和FLAG_ACTIVITY_NEW_DOCUMENT说明-爱代码爱编程
其实“最近使用的应用”屏幕添加链接描述 这篇文章已经写的很清楚了,但是网上的很多介绍都是翻译的api文档,让人看了一头雾水,这里再给大家科普一下。
首先要知道Android的最近任务里面的显示是以任务为单位的, 一般清空下Android程序按照应用按照应用包名来组织task, 或者按照taskAffinity来组织逻辑上的应用。 同一个Activity不能单独的出现在不同的activity task中,也就是没法在多任务中看到多个任务。 这显然是满足用户使用体验的,比如用户使用WPS打开两篇文档,如果把这两篇文档放在同一个task中,就只能销毁其中一篇才能看到另外一篇。为了解决这个问题Android 5.0开始引入documentLaunchMode。有了documentLaunchMode,如果同一 Activity 的多个实例包含不同的文档,它们可能会在最近使用的应用屏幕中显示为任务。比如说,Google 云端硬盘的几个 Google 文档可能各自都有一个任务。每个文档都会在最近使用的应用屏幕中显示为任务。
所以documentLaunchMode的目的就是将相同的Activity启动到不同的task中,以便在最近任务中进行切换。
知道了documentLaunchMode的设计就好理解documentLaunchMode的作用了
要了解上面的含义还是需要知道FLAG_ACTIVITY_MULTIPLE_TASK标志的作用, FLAG_ACTIVITY_MULTIPLE_TASK标志强制将Activity启动到一个新的task, 所以intoExisting模式其实就是如果已经存在可复用的Activity则不会启动新的Task。 always表示创建Activity总是到新的Task中。 none模式表示和普通的standard行为一致。 never标志表示即使启动该Activity设置了FLAG_ACTIVITY_NEW_DOCUMENT和FLAG_ACTIVITY_MULTIPLE_TASK标记也不会将Activity启动到单独的Task中。
再来看下FLAG_ACTIVITY_NEW_DOCUMENT 标志, 其实就是FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET标志,在Android 5.0开始该标志已经作废,我们可以看下4.4中的定义。
/**
* This flag is used to open a document into a new task rooted at the activity launched
* by this Intent. Through the use of this flag, or its equivalent attribute,
* {@link android.R.attr#documentLaunchMode} multiple instances of the same activity
* containing different documents will appear in the recent tasks list.
*
* <p>The use of the activity attribute form of this,
* {@link android.R.attr#documentLaunchMode}, is
* preferred over the Intent flag described here. The attribute form allows the
* Activity to specify multiple document behavior for all launchers of the Activity
* whereas using this flag requires each Intent that launches the Activity to specify it.
*
* <p>Note that the default semantics of this flag w.r.t. whether the recents entry for
* it is kept after the activity is finished is different than the use of
* {@link #FLAG_ACTIVITY_NEW_TASK} and {@link android.R.attr#documentLaunchMode} -- if
* this flag is being used to create a new recents entry, then by default that entry
* will be removed once the activity is finished. You can modify this behavior with
* {@link #FLAG_ACTIVITY_RETAIN_IN_RECENTS}.
*
* <p>FLAG_ACTIVITY_NEW_DOCUMENT may be used in conjunction with {@link
* #FLAG_ACTIVITY_MULTIPLE_TASK}. When used alone it is the
* equivalent of the Activity manifest specifying {@link
* android.R.attr#documentLaunchMode}="intoExisting". When used with
* FLAG_ACTIVITY_MULTIPLE_TASK it is the equivalent of the Activity manifest specifying
* {@link android.R.attr#documentLaunchMode}="always".
*
* Refer to {@link android.R.attr#documentLaunchMode} for more information.
*
* @see android.R.attr#documentLaunchMode
* @see #FLAG_ACTIVITY_MULTIPLE_TASK
*/
public static final int FLAG_ACTIVITY_NEW_DOCUMENT = FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
也就是说从桌面重新恢复关联任务后,会清除document类型的activity,防止用户不知所措(多任务不会)。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/woai110120130/article/details/111092009