Intent的Flag总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu_zi/article/details/52220206

最近在项目的开发中通过使用Intent.setFlag()使得开发得到了很大的方便,所以对常用的Flag做了一下总结。


首先下面是Android源码中对于setFlag方法的解释

 /**
     * Set special flags controlling how this intent is handled.  Most values
     * here depend on the type of component being executed by the Intent,
     * specifically the FLAG_ACTIVITY_* flags are all for use with
     * {@link Context#startActivity Context.startActivity()} and the
     * FLAG_RECEIVER_* flags are all for use with
     * {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}.
     *
     * <p>See the
     * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
     * Stack</a> documentation for important information on how some of these options impact
     * the behavior of your application.
     *
     * @param flags The desired flags.
     *
     * @return Returns the same Intent object, for chaining multiple calls
     * into a single statement.
     *
     * @see #getFlags
     * @see #addFlags
     *
     * @see #FLAG_GRANT_READ_URI_PERMISSION
     * @see #FLAG_GRANT_WRITE_URI_PERMISSION
     * @see #FLAG_GRANT_PERSISTABLE_URI_PERMISSION
     * @see #FLAG_GRANT_PREFIX_URI_PERMISSION
     * @see #FLAG_DEBUG_LOG_RESOLUTION
     * @see #FLAG_FROM_BACKGROUND
     * @see #FLAG_ACTIVITY_BROUGHT_TO_FRONT
     * @see #FLAG_ACTIVITY_CLEAR_TASK
     * @see #FLAG_ACTIVITY_CLEAR_TOP
     * @see #FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
     * @see #FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
     * @see #FLAG_ACTIVITY_FORWARD_RESULT
     * @see #FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
     * @see #FLAG_ACTIVITY_MULTIPLE_TASK
     * @see #FLAG_ACTIVITY_NEW_DOCUMENT
     * @see #FLAG_ACTIVITY_NEW_TASK
     * @see #FLAG_ACTIVITY_NO_ANIMATION
     * @see #FLAG_ACTIVITY_NO_HISTORY
     * @see #FLAG_ACTIVITY_NO_USER_ACTION
     * @see #FLAG_ACTIVITY_PREVIOUS_IS_TOP
     * @see #FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
     * @see #FLAG_ACTIVITY_REORDER_TO_FRONT
     * @see #FLAG_ACTIVITY_SINGLE_TOP
     * @see #FLAG_ACTIVITY_TASK_ON_HOME
     * @see #FLAG_RECEIVER_REGISTERED_ONLY
     */
    public Intent setFlags(int flags) {
        mFlags = flags;
        return this;
    }

这段英文也还比较容易看明白,它说FLAG_ACTIVITY_*是用于Context.startActivity(),FLAG_RECEIVER_*是用于Context.sendBroadcast(),然后下面列举了一些常量,点击常量可以跳转到常量的声明的地方,可以看到常量的解释。我们就看看它列举的一些常用常量分别表示什么含义。


FLAG_FROM_BACKGROUND

表示这个intent可以通过一个来自后台的操作去调用它,不是直接与用户交互的。


FLAG_ACTIVITY_CLEAR_TASK

 /**
     * If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
     * 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}.
     */

如果在使用Context.startActivity的时候设置这个flag,将导致这个栈中任何与此Activity相关联的栈都被清除,在这个Activity启动成功之前。所以这个Activity就成了一个新栈中最底层的Activity,其他的所有的旧的Activity都被结束掉了。这个只能与FLAG_ACTIVITY_NEW_TASK一起使用。

例如我有MainActivity,AActivity,BActivity,CActivity,DActivity,跳转顺序main-->A-->B-->C--D,我在DActivity里跳转到BActivity里。跳转代码如下:

intent = new Intent(this, BActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

在BActivity里按返回键程序就直接退出了。这表示此时任务栈里只有BActivity了。


FLAG_ACTIVITY_CLEAR_TOP

还是刚刚的例子,跳转顺序main-->A-->B-->C--D,我在DActivity里跳转到BActivity里,如果intent设置的flag是FLAG_ACTIVITY_CLEAR_TOP的话,此时任务栈里的C和D会被清除掉,此时任务栈里只剩下Main、A、B,这3个Activity。另外如果BActivity的launchMode是singleTask或singleInstance或singleTop的话,此时B界面保存的内容还在,并且会回调onNewIntent方法。如果BActivity的launchMode是standard的话BActivity保存的内容已经没有了,也没有回调onNewIntent()的方法,它是结束了之前的BActivity在重新创建了一个BActivity。


FLAG_ACTIVITY_NO_HISTORY

还是上面的例子,跳转顺序main-->A-->B-->C--D,如果在B-->C的时候Intent设置的Flag是FLAG_ACTIVITY_NO_HISTORY,那么,在D按返回键的时候回直接退回到B界面。C没有存在任务栈中。


FLAG_ACTIVITY_SINGLE_TOP

Intent设置了这个Flag的,相当于目标Activity设置了SingleTop

猜你喜欢

转载自blog.csdn.net/wu_zi/article/details/52220206