现在的位置: 首页 > 综合 > 正文

FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP

2019年10月09日 ⁄ 综合 ⁄ 共 4439字 ⁄ 字号 评论关闭

【希望大家分享一下对这两个flag的用法、谢谢】

先看一下这两个的官方解释

一、FLAG_ACTIVITY_SINGLE_TOP

/**
     * If set, the activity will not be launched if it is already running
     * at the top of the history stack.
     */
    public static final int FLAG_ACTIVITY_SINGLE_TOP = 0x20000000;

个人理解为:

startActivity如果设置flag为 FLAG_ACTIVITY_SINGLE_TOP 

当目标activity在stack的顶部的时候 不会重新被创建

二、FLAG_ACTIVITY_CLEAR_TOP 

看官方解释 

如果设置了FLAG_ACTIVITY_CLEAR_TOP 并且。目标Activity已经在当前的task中启动。那么不会launch一个新的Activity实例。

同时目标Activity之上的所有Activity将会被destroy 并且目标Activity会接受一个新的intent 

?????????????可是用过之后感觉我理解错了。。。。

  /**
     * 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.
     *
     * <p>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.
     *
     * <p>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 {@link #FLAG_ACTIVITY_SINGLE_TOP} in
     * the same intent, then it will be finished and re-created; for all other
     * launch modes or if {@link #FLAG_ACTIVITY_SINGLE_TOP} is set then this
     * Intent will be delivered to the current instance's onNewIntent().
     *
     * <p>This launch mode can also be used to good effect in conjunction with
     * {@link #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.
     *
     * <p>See
     * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
     * Stack</a> for more information about tasks.
     */
    public static final int FLAG_ACTIVITY_CLEAR_TOP = 0x04000000;

假设三个Activity  A B C

如果A启动B  B启动C。此时 task中的顺序   ABC  栈顶是C

如果在后台一个Service中启动B

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setClass(BeatService.this, Activity_B.class);
startActivity(intent);

我最开始的理解是  task中 由 ABC 变成 ACB 

但是调试发现 此时重新创建了一个B

task中变成 ABCB 

如果此时在service中再次启动B 则不会重新创建,仅仅执行onResume

11-03 14:44:52.410: D/activity(26579): onCreate Activity_A
11-03 14:44:52.410: D/activity(26579): onResume Activity_A
11-03 14:44:53.490: D/activity(26579): onCreate Activity_B
11-03 14:44:53.490: D/activity(26579): onResume Activity_B
11-03 14:44:53.760: D/activity(26579): onStop Activity_A
11-03 14:44:54.350: D/activity(26579): onCreate Activity_C
11-03 14:44:54.350: D/activity(26579): onResume Activity_C
11-03 14:44:54.630: D/activity(26579): onStop Activity_B
11-03 14:44:56.050: D/activity(26579): onCreate Activity_B  /// 在service中启动B
11-03 14:44:56.050: D/activity(26579): onResume Activity_B
11-03 14:44:56.330: D/activity(26579): onStop Activity_C
11-03 14:44:58.540: D/activity(26579): onResume Activity_B  //  B在栈顶,此时仅仅执行onResume

二、FLAG_ACTIVITY_CLEAR_TOP 启动模式

假设task中三个activity   ABC

此时后台Service中启动B。代码如下

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setClass(BeatService.this, Activity_B.class);
startActivity(intent);

task中activity的变化为  ABC -- 销毁B 创建B 销毁C

此时task中剩下AB 如果再次启动B

则会创建B 销毁B 此时task中还是AB

两种情况B的创建、销毁顺序是不一样的。

11-03 14:47:47.430: D/activity(27722): onCreate Activity_A //启动A
11-03 14:47:47.430: D/activity(27722): onResume Activity_A
11-03 14:47:51.190: D/activity(27722): onCreate Activity_B //A中启动B
11-03 14:47:51.200: D/activity(27722): onResume Activity_B
11-03 14:47:51.460: D/activity(27722): onStop Activity_A
11-03 14:47:52.040: D/activity(27722): onCreate Activity_C //B中启动C
11-03 14:47:52.040: D/activity(27722): onResume Activity_C
11-03 14:47:52.500: D/activity(27722): onStop Activity_B
11-03 14:47:54.630: D/activity(27722): onDestroy Activity_B // Service中启动B
11-03 14:47:54.680: D/activity(27722): onCreate Activity_B
11-03 14:47:54.680: D/activity(27722): onResume Activity_B
11-03 14:47:55.040: D/activity(27722): onStop Activity_C
11-03 14:47:55.040: D/activity(27722): onDestroy Activity_C // Service中启动B 结束
11-03 14:47:57.650: D/activity(27722): onCreate Activity_B  //service中再次启动B
11-03 14:47:57.650: D/activity(27722): onResume Activity_B
11-03 14:47:57.920: D/activity(27722): onStop Activity_B
11-03 14:47:57.920: D/activity(27722): onDestroy Activity_B

demo的代码上传在

https://github.com/droidcoffee/yolanda/tree/master/yolanda_Android

抱歉!评论已关闭.