Intent.addFlags() 启动Activity的20种flags全解析
Activity是四大组件中最重要的一个,也是平时开发中接触最多的。与Activity启动行为相关的就是它的启动模式,Standard、SingleTop、SingleTask、SingleInstance这4种launchMode相信大家不陌生,如果不太熟悉可以看这里:
https://www.jianshu.com/p/c34483bb5c0f
OK,熟悉了4种launchMode后相信你已经对Activity任务栈有一些理解了,能应对一些常见的开发场景了。然而,除了4种launchMode,还有更复杂的控制Activity行为的方式,那就是给启动Activity的Intent添加flag,使用 Intent.addFlags(int flags) 方法。看一下这个方法的参数,总共有20种:
/**
* 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}.
*/
/**
* 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.
*
* 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.
*
*
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().
*
*
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.
*
*
See
* Tasks and Back
* Stack for more information about tasks.
*/
/**
* @deprecated As of API 21 this performs identically to
* {@link #FLAG_ACTIVITY_NEW_DOCUMENT} which should be used instead of this.
*/
/**
* This flag is used to create a new task and launch an activity into it.
* This flag is always paired with either {@link #FLAG_ACTIVITY_NEW_DOCUMENT}
* or {@link #FLAG_ACTIVITY_NEW_TASK}. In both cases these flags alone would
* search through existing tasks for ones matching this Intent. Only if no such
* task is found would a new task be created. When paired with
* FLAG_ACTIVITY_MULTIPLE_TASK both of these behaviors are modified to skip
* the search for a matching task and unconditionally start a new task.
*
* When used with {@link #FLAG_ACTIVITY_NEW_TASK} do not use this
* flag unless you are implementing your own
* top-level application launcher. Used in conjunction with
* {@link #FLAG_ACTIVITY_NEW_TASK} to disable the
* behavior of bringing an existing task to the foreground. When set,
* a new task is always started to host the Activity for the
* Intent, regardless of whether there is already an existing task running
* the same thing.
*
* Because the default system does not include graphical task management,
* you should not use this flag unless you provide some way for a user to
* return back to the tasks you have launched.
*
* See {@link #FLAG_ACTIVITY_NEW_DOCUMENT} for details of this flag's use for
* creating new document tasks.
*
*
This flag is ignored if one of {@link #FLAG_ACTIVITY_NEW_TASK} or
* {@link #FLAG_ACTIVITY_NEW_DOCUMENT} is not also set.
*
*
See
* Tasks and Back
* Stack for more information about tasks.
*
* @see #FLAG_ACTIVITY_NEW_DOCUMENT
* @see #FLAG_ACTIVITY_NEW_TASK
*/
/**
* 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.
*
* 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.
*
*
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}.
*
*
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
*/
/**
* 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. See
* Tasks and Back
* Stack for more information about tasks.
*
* This flag is generally used by activities that want
* to present a "launcher" style behavior: they give the user a list of
* separate things that can be done, which otherwise run completely
* independently of the activity launching them.
*
*
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. See {@link #FLAG_ACTIVITY_MULTIPLE_TASK} for a flag
* to disable this behavior.
*
*
This flag can not be used when the caller is requesting a result from
* the activity being launched.
*/
/**
* If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
* this flag will prevent the system from applying an activity transition
* animation to go to the next activity state. This doesn't mean an
* animation will never run -- if another activity change happens that doesn't
* specify this flag before the activity started here is displayed, then
* that transition will be used. This flag can be put to good use
* when you are going to do a series of activity operations but the
* animation seen by the user shouldn't be driven by the first activity
* change but rather a later one.
*/
/**
* If set, the new activity is not kept in the history stack. As soon as
* the user navigates away from it, the activity is finished. This may also
* be set with the {@link android.R.styleable#AndroidManifestActivity_noHistory
* noHistory} attribute.
*
* If set, {@link android.app.Activity#onActivityResult onActivityResult()}
* is never invoked when the current activity starts a new activity which
* sets a result and finishes.
*/
/**
* If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
* this flag will cause the launched activity to be brought to the front of its
* task's history stack if it is already running.
*
* For example, consider a task consisting of four activities: A, B, C, D.
* If D calls startActivity() with an Intent that resolves to the component
* of activity B, then B will be brought to the front of the history stack,
* with this resulting order: A, C, D, B.
*
* This flag will be ignored if {@link #FLAG_ACTIVITY_CLEAR_TOP} is also
* specified.
*/
/**
* By default a document created by {@link #FLAG_ACTIVITY_NEW_DOCUMENT} will
* have its entry in recent tasks removed when the user closes it (with back
* or however else it may finish()). If you would like to instead allow the
* document to be kept in recents so that it can be re-launched, you can use
* this flag. When set and the task's activity is finished, the recents
* entry will remain in the interface for the user to re-launch it, like a
* recents entry for a top-level application.
*
* The receiving activity can override this request with
* {@link android.R.attr#autoRemoveFromRecents} or by explcitly calling
* {@link android.app.Activity#finishAndRemoveTask()
* Activity.finishAndRemoveTask()}.
*/
/**
* If set, the activity will not be launched if it is already running
* at the top of the history stack.
*/
/**
* If set and this intent is being used to launch a new activity from an
* existing one, then the reply target of the existing activity will be
* transfered to the new activity. This way the new activity can call
* {@link android.app.Activity#setResult} and have that result sent back to
* the reply target of the original activity.
*/
/**
* If set and this intent is being used to launch a new activity from an
* existing one, the current activity will not be counted as the top
* activity for deciding whether the new intent should be delivered to
* the top instead of starting a new one. The previous activity will
* be used as the top, with the assumption being that the current activity
* will finish itself immediately.
*/
/**
* If set, the new activity is not kept in the list of recently launched
* activities.
*/
/**
* This flag is not normally set by application code, but set for you by
* the system as described in the
* {@link android.R.styleable#AndroidManifestActivity_launchMode
* launchMode} documentation for the singleTask mode.
*/
/**
* If set, and this activity is either being started in a new task or
* bringing to the top an existing task, then it will be launched as
* the front door of the task. This will result in the application of
* any affinities needed to have that task in the proper state (either
* moving activities to or from it), or simply resetting that task to
* its initial state if needed.
*/
/**
* This flag is not normally set by application code, but set for you by
* the system if this activity is being launched from history
* (longpress home key).
*/
/**
* If set, this flag will prevent the normal {@link android.app.Activity#onUserLeaveHint}
* callback from occurring on the current frontmost activity before it is
* paused as the newly-started activity is brought to the front.
*
* Typically, an activity can rely on that callback to indicate that an
* explicit user action has caused their activity to be moved out of the
* foreground. The callback marks an appropriate point in the activity's
* lifecycle for it to dismiss any notifications that it intends to display
* "until the user has seen them," such as a blinking LED.
*
*
If an activity is ever started via any non-user-driven events such as
* phone-call receipt or an alarm handler, this flag should be passed to {@link
* Context#startActivity Context.startActivity}, ensuring that the pausing
* activity does not think the user has acknowledged its notification.
*/
/**
* If set in an Intent passed to {@link Context#startActivity Context.startActivity()},
* this flag will cause a newly launching task to be placed on top of the current
* home activity task (if there is one). That is, pressing back from the task
* will always return the user to home even if that was not the last activity they
* saw. This can only be used in conjunction with {@link #FLAG_ACTIVITY_NEW_TASK}.
*/
/**
* This flag is only used in split-screen multi-window mode. The new activity will be displayed
* adjacent to the one launching it. This can only be used in conjunction with
* {@link #FLAG_ACTIVITY_NEW_TASK}. Also, setting {@link #FLAG_ACTIVITY_MULTIPLE_TASK} is
* required if you want a new instance of an existing activity to be created.
*/
1. 本flag只在分屏多窗口模式下使用。新活动会显示在旧活动旁边。本flag只能跟FLAG_ACTIVITY_NEW_TASK联合使用。并且如果你想创建一个已存在活动的新实例,那么要设置FLAG_ACTIVITY_MULTIPLE_TASK。
public static final int FLAG_ACTIVITY_LAUNCH_ADJACENT = 0x00001000