IntentIntent.FLAG_ACTIVITY_NEW_TASK的作用?


Android版本:4.2.2

Intent.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. See 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.

对画线部分的理解:设置了此标志后,启动该intent的Activity前,如果已经有一个在运行的task包含这个Activity的实例,则不会再创建新的Activity实例,而是仅仅将该task带到前台,并且保留它(进入后台前)最后的状态;否则创建一个新的task,该Activity的实例作为这个task的第一个。

测试准备:创建了工程 TestA 和 TestB,工程 TestA 中包含两个 MainActivity 和 SecondActivity,其中 MainActivity 在清单文件中设置了意图过滤器;TestB 中只有一个 MainActivity ,包含一个按钮,点击按钮用隐式意图启动 TestA 的 MainActivity,intent设置了Intent.FLAG_ACTIVITY_NEW_TASK;

测试过程:先启动 TestA ,通过 TestA 中的按钮跳转到 SecondActivity,然后按 HOME 键返回到桌面;启动 TestB,点击按钮启动 TestA 的

MainActivity。

测试预测:按照 API 的解释,应该是 TestA 的 Task 被带到前台而已,不会创建新的 MainActivity 。

测试结果:TestA 的 Task 确实被带到了前台,但是新创建了一个 MainActivity 放到栈顶。

有大神路过的话,跪求解释!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值