Activity的启动模式与Intent的Flag详解以及Activity任务栈查询

一、Activity栈的查询命令:在cmd中输入adb shell dumpsys activity activities即可。


############################################################################################

二、Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance。

standard:Activity的默认加载方法,栈1:A B C D ,D跳转到D,变为:A B C D D。又跳转到B,则变为:A B C D D B。此时如果依次按返回键,D  D C B A将会依次弹出栈而显示在界面上。

singleTop:如果Activity设置成singleTop,那么当该Activity位于栈顶的时候,再通过Intent跳转到本身这个   Activity,则将不会创建一个新的实例压入栈中。即栈1:A B C D,D的Launch mode为singleTop,   D跳转到D,栈的情况依然为:A B C D。但是如果此时B的模式也是singleTop,D跳转到B,那么则   会新建一个B的实例压入栈中,因为B不是位于栈顶,此时栈的情况就变成了:A B C D B。

singleTask:如果Activity设置为singleTask,那么Task栈中只存在一个该Activity的实例。如:栈1:A B C D。B     的Launch mode为singleTask,D跳转到B,则栈的情况为:A B。而C和D被弹出销毁了,也就是     说位于B之上的实例都被销毁了。

singleInstance:所有Task栈中只能有一个该Activity的实例,也就是如果原本有就调用原本的,如果原本没有,   就创建新的

############################################################################################

三、Intent中的Flag相关属性

  • FLAG_GRANT_READ_URI_PERMISSION
    public static final int FLAG_GRANT_READ_URI_PERMISSION
    If set, the recipient of this Intent will be granted permission to perform read operations on the Uri in the Intent's data and any URIs specified in its ClipData. When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.
  • FLAG_GRANT_WRITE_URI_PERMISSION
    public static final int FLAG_GRANT_WRITE_URI_PERMISSION
    If set, the recipient of this Intent will be granted permission to perform write operations on the Uri in the Intent's data and any URIs specified in its ClipData. When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.
  • FLAG_FROM_BACKGROUND
    public static final int FLAG_FROM_BACKGROUND
    Can be set by the caller to indicate that this Intent is coming from a background operation, not from direct user interaction.
  • FLAG_DEBUG_LOG_RESOLUTION
    public static final int FLAG_DEBUG_LOG_RESOLUTION
    A flag you can enable for debugging: when set, log messages will be printed during the resolution of this intent to show you what has been found to create the final resolved list.
  • FLAG_EXCLUDE_STOPPED_PACKAGES
    public static final int FLAG_EXCLUDE_STOPPED_PACKAGES
    If set, this intent will not match any components in packages that are currently stopped. If this is not set, then the default behavior is to include such applications in the result.
  • FLAG_INCLUDE_STOPPED_PACKAGES
    public static final int FLAG_INCLUDE_STOPPED_PACKAGES
    If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).
  • FLAG_ACTIVITY_NO_HISTORY
    public static final int FLAG_ACTIVITY_NO_HISTORY
    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 noHistory attribute.
  • FLAG_ACTIVITY_SINGLE_TOP
    public static final int FLAG_ACTIVITY_SINGLE_TOP
    If set, the activity will not be launched if it is already running at the top of the history stack.
  • FLAG_ACTIVITY_NEW_TASK
    public static final int 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. 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. SeeFLAG_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.

  • FLAG_ACTIVITY_MULTIPLE_TASK
    public static final int FLAG_ACTIVITY_MULTIPLE_TASK
    Do not use this flag unless you are implementing your own top-level application launcher. Used in conjunction with 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.

    This flag is ignored if FLAG_ACTIVITY_NEW_TASK is not set.

    See Tasks and Back Stack for more information about tasks.

  • FLAG_ACTIVITY_CLEAR_TOP
    public static final int FLAG_ACTIVITY_CLEAR_TOP
    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 FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or ifFLAG_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 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.

  • FLAG_ACTIVITY_FORWARD_RESULT
    public static final int FLAG_ACTIVITY_FORWARD_RESULT
    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 Activity.setResult(int) and have that result sent back to the reply target of the original activity.
  • FLAG_ACTIVITY_PREVIOUS_IS_TOP
    public static final int FLAG_ACTIVITY_PREVIOUS_IS_TOP
    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.
  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    public static final int FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    If set, the new activity is not kept in the list of recently launched activities.

  • FLAG_ACTIVITY_BROUGHT_TO_FRONT
    public static final int FLAG_ACTIVITY_BROUGHT_TO_FRONT
    This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.
  • FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
    public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
    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.
  • FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
    public static final int FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
    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).
  • FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
    public static final int FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
    If set, this marks a point in the task's activity stack that should be cleared when the task is reset. That is, the next time the task is brought to the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (typically as a result of the user re-launching it from home), this activity and all on top of it will be finished so that the user does not return to them, but instead returns to whatever activity preceeded it.

    This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.

  • FLAG_ACTIVITY_NO_USER_ACTION
    public static final int FLAG_ACTIVITY_NO_USER_ACTION
    If set, this flag will prevent the normal 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 toContext.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.

  • FLAG_ACTIVITY_REORDER_TO_FRONT
    public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
    If set in an Intent passed to 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 FLAG_ACTIVITY_CLEAR_TOP is also specified.

  • FLAG_ACTIVITY_NO_ANIMATION
    public static final int FLAG_ACTIVITY_NO_ANIMATION
    If set in an Intent passed to 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.
  • FLAG_ACTIVITY_CLEAR_TASK
    public static final int FLAG_ACTIVITY_CLEAR_TASK
    If set in an Intent passed to 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 FLAG_ACTIVITY_NEW_TASK.
  • FLAG_ACTIVITY_TASK_ON_HOME
    public static final int FLAG_ACTIVITY_TASK_ON_HOME
    If set in an Intent passed to 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 FLAG_ACTIVITY_NEW_TASK.
  • FLAG_RECEIVER_REGISTERED_ONLY
    public static final int FLAG_RECEIVER_REGISTERED_ONLY
    If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.
  • FLAG_RECEIVER_REPLACE_PENDING
    public static final int FLAG_RECEIVER_REPLACE_PENDING
    If set, when sending a broadcast the new broadcast will replace any existing pending broadcast that matches it. Matching is defined by Intent.filterEquals returning true for the intents of the two broadcasts. When a match is found, the new broadcast (and receivers associated with it) will replace the existing one in the pending broadcast list, remaining at the same position in the list.

    This flag is most typically used with sticky broadcasts, which only care about delivering the most recent values of the broadcast to their receivers.

  • FLAG_RECEIVER_FOREGROUND
    public static final int FLAG_RECEIVER_FOREGROUND
    If set, when sending a broadcast the recipient is allowed to run at foreground priority, with a shorter timeout interval. During normal broadcasts the receivers are not automatically hoisted out of the background priority class.
  • FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
    public static final int FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
    If set, when sending a broadcast before boot has completed only registered receivers will be called -- no BroadcastReceiver components will be launched. Sticky intent state will be recorded properly even if no receivers wind up being called. If FLAG_RECEIVER_REGISTERED_ONLY is specified in the broadcast intent, this flag is unnecessary.

    This flag is only for use by system sevices as a convenience to avoid having to implement a more complex mechanism around detection of boot completion.

  • FLAG_RECEIVER_BOOT_UPGRADE
    public static final int FLAG_RECEIVER_BOOT_UPGRADE
    Set when this broadcast is for a boot upgrade, a special mode that allows the broadcast to be sent before the system is ready and launches the app process with no providers running in it.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值