Android Intent 常用的Flag

Activity的启动模式可以在AndroidManifest.xml中定义launchMode,也可以通过intent.setFlag(int flags)或者intent.addFlag(int flags)进行设置,这里主要讲常用的几种Flag方便日后使用(不对的地方还望指正).

Android Intent 常用的Flag有以下几种:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK

FLAG_ACTIVITY_NEW_TASK

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.
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.
public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;
FLAG_ACTIVITY_NEW_TASK如果设置了这个flag,这个activity将会成为历史栈里面的新任务,什么是任务呢?任务是指在执行特定作业时与用户交互的一系列 Activity。 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈)中。
当使用这个flag的时候,在新任务中启动 Activity。如果已为正在启动的 Activity 运行任务,则该任务会转到前台并恢复其最后状态,同时 Activity 会在 onNewIntent() 中收到新 Intent。
这里测试发现,如果launchMode为default的activity,仅设置FLAG_ACTIVITY_NEW_TASK flag,还是会创建新的activity实例.

FLAG_ACTIVITY_CLEAR_TOP

(注释的api很长,这里就复制粘贴了)
如果正在启动的 Activity 已在当前任务中运行,则会销毁当前任务顶部的所有 Activity,并通过 onNewIntent() 将此 Intent 传递给 Activity 已恢复的实例(现在位于顶部),而不是启动该 Activity 的新实例。
产生这种行为的 launchMode 属性没有值。
FLAG_ACTIVITY_CLEAR_TOP 通常与 FLAG_ACTIVITY_NEW_TASK 结合使用。一起使用时,通过这些标志,可以找到其他任务中的现有 Activity,并将其放入可从中响应 Intent 的位置。例如在notification manager 中启动activity.
FLAG_ACTIVITY_CLEAR_TOP 通常与 FLAG_ACTIVITY_NEW_TASK 结合使用,在API中的文档这样说的: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.

FLAG_ACTIVITY_SINGLE_TOP

If set, the activity will not be launched if it is already running at the top of the history stack.
如果正在启动的 Activity 是当前 Activity(位于返回栈的顶部),则 现有实例会接收对 onNewIntent() 的调用,而不是创建 Activity 的新实例。
这会产生与 "singleTop"launchMode 值相同的行为。

FLAG_ACTIVITY_CLEAR_TASK

this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.This can only be used in conjunction with {@link #FLAG_ACTIVITY_NEW_TASK}.
这个flag会在启动这个activity之前,清理掉与这个activity相关的任何任务.也就是说,这个activity会在其他空任务中的成为新根activity,且任何旧的activity会被销毁. 注意,这个flag只能和FLAG_ACTIVITY_NEW_TASK结合使用.
FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TASK结合使用的场景场景:调起第三方的app,使得第三方的app从launcher页面重新打开(就像杀掉app,重新进来app一样).(我的项目中有用到)

如果想以singleTask的模式启动一个launchMode为standard的activity,可以这么写:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
有人说使用intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP),但是是不对的,因为这两者联合使用的时候,如果指定 Activity 的启动模式为 “standard”,则该 Activity 也会从堆栈中移除,并在其位置启动一个新实例,以便处理传入的 Intent。 这是因为当启动模式为 “standard” 时,将始终为新 Intent 创建新实例。–来自安卓中文开发文档https://developer.android.com/guide/components/tasks-and-back-stack?hl=zh-CN

也有人说普通的Activity以singleTask模式启动需要使用(FLAG_ACTIVITY_SINGLE_TOP | FLAG_ACITIVTY_CLEAR_TOP),经过测试部分手机,如果app从未开启过或者被杀死startActivity()之后无法调起activity.

参考:
https://developer.android.com/guide/components/tasks-and-back-stack?hl=zh-CN
https://blog.csdn.net/wzy_1988/article/details/78240734

更多相关文章

  1. No.11 使用firewall配置的防火墙策略的生效模式
  2. Android-NDK开发之基础--Android(安卓)JNI实例代码(二)-- 获取/
  3. Android的蓝牙实例代码中找不到REQUEST_ENABLE_BT
  4. Android(安卓)camera系统开发之IPC (四)
  5. [Android]文件操作模式与SDCard读写访问
  6. 【Android】点击应用进而打开base.apk的调用流程再分析
  7. Android(安卓)Studio程序无法加载到虚拟机解决方法
  8. 贴一个定义android animation的应用实例
  9. Android怎么获取ListView的值

随机推荐

  1. Android(安卓)命令行手动编译打包过程图
  2. Android监控外接USB设备和获取USB等设备
  3. 5分钟理解Android中的上下文!!!
  4. android双击返回键退出程序
  5. 收藏的Android非常好用的组件或者框架
  6. Android(安卓)CheckBox
  7. Android(安卓)TextView文字过多时,通过滚
  8. android imageswitcher gallery 根据数据
  9. Android(安卓)通过WebView 调用Js sqlite
  10. android各组件详解