Intent-- FLAG_ACTIVITY_CLEAR_TOP

原创 2016年05月31日 11:52:52

今天在项目中接触到了这么一个关键字,记录下来以备以后查看。

首先来看代码:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

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.
     */

解释很简单,如果intent设置了这个FLAG,假设需要新启动的Activity为B,并且任务栈中已经存在B的实例,那么会将任务栈中B之上的所有Activity(如果存在)结束。此时,任务栈栈顶的Activity为一个接收到了intent信息的B。

举例说明,假设任务栈中有四个Activity,分别为A-B-C-D(栈顶为D)。此时D调用startActivity() 方法去生成B,此时任务栈中的C和D将会被结束,栈顶为接收到了Intent的B。任务栈中剩余的Activity为A-B。

最后上例中B接收intent的方式取决于它的启动模式,当他的启动模式为标准模式时,B会结束自身在重新创建新的实例来接收intent。而其他启动模式,则不会创建新的实例,而是在onNewIntent()方法中接收intent信息。

Android 关闭多个视图Intent.FLAG_ACTIVITY_CLEAR_TOP用法

如果已经启动了四个Activity:A,B,C和D。在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的inten...
  • LVXIANGAN
  • LVXIANGAN
  • 2014-12-24 10:50:12
  • 31018

关于Android的Intent.FLAG_ACTIVITY_CLEAR_TOP

今天写代码遇到了一个问题:有如下几个Activity A - B - C 当 B - A - B 跳转的时候,使用Intent的FLAG_ACTIVITY_CLEAR_TOP会让第一个B和第二个A,d...
  • h3c4lenovo
  • h3c4lenovo
  • 2013-12-12 17:11:46
  • 22680

singTask和FLAG_ACTIVITY_CLEAR_TOP的区别

假设所有的activity启动方式是standard,两者的区别就是1. intent中的flag为FLAG_ACTIVITY_CLEAR_TOP时,将指定activity上面的其他activity出...
  • the_Sunshine_of_King
  • the_Sunshine_of_King
  • 2017-05-06 14:59:01
  • 1634

FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP

假设三个Activity  A B C rugu
  • ID19870510
  • ID19870510
  • 2014-11-03 14:58:40
  • 3053

Android的Intent.FLAG_ACTIVITY_CLEAR_TOP无效

今天写代码遇到了一个问题: 当 B - A - B 跳转的时候,使用Intent的FLAG_ACTIVITY_CLEAR_TOP会让第一个B和第二个A,destory掉,但是当B - A - C...
  • u011361576
  • u011361576
  • 2015-09-21 14:39:56
  • 2900

FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_CLEAR_TOP :如果设置,并且要启动的activity已经在当前的任务中,那么在该activity之上的activity都会关闭,并且intent会传递给老的acti...
  • new_abc
  • new_abc
  • 2013-10-30 20:59:56
  • 2869

Intent.FLAG_ACTIVITY_CLEAR_TOP 的使用注意

最近开发一个下载的应用,然后有很多层跳转关系,跳到最后进行下载,下载完毕之后弹出一个按钮,点击之后会将所有activity都结束掉。一开始用的方法是 Intent intent = new In...
  • u011487510
  • u011487510
  • 2015-06-24 09:26:04
  • 1919

Activity的启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT

注:. 四种:         1. standard             被启动就会创建一个新的   2. singleTop            栈顶单实例(当该a...
  • u012207345
  • u012207345
  • 2017-06-20 11:47:09
  • 1966

Android 启动模式之 FLAG_ACTIVITY_CLEAR_TOP

以前对于activity的启动模式没有特别在意,最近在项目中遇到了麻烦事,对于启动模式又去了解一遍,除了常用的4种启动模式,还有一些特别的启动flag,用处也特别大最近项目遇到一个麻烦的事情,做And...
  • behindeye
  • behindeye
  • 2017-06-11 00:54:07
  • 210

.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));知多少

1.Bug背景   老版张朝阳提的Bug,组长分配给了我,开始了各种苦逼的研究,此处不表……  2.Bug描述     杀死搜狐新闻客户端进程,重新分享到搜狐我的,分享的落地页就打不开,点击无任...
  • kaiqiangzhang001
  • kaiqiangzhang001
  • 2016-04-06 15:44:17
  • 640
收藏助手
不良信息举报
您举报文章:Intent-- FLAG_ACTIVITY_CLEAR_TOP
举报原因:
原因补充:

(最多只允许输入30个字)