android 清空之前task中的activity

开发过程有个场景:关闭之前所有activity,跳转到登录页

以前是activity都放到一个list里面,退出应用的时候遍历列表,逐个finish

换种实现方式

跳转登录页的intent添加两个flag
Intent.FLAG_ACTIVITY_CLEAR_TASK

Intent.FLAG_ACTIVITY_NEW_TASK


Intent intent = new Intent(this, TestCActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);



当然如果登录页的launchMode为singleTask的话,就不需要
Intent.FLAG_ACTIVITY_NEW_TASK

(要么在manifest文件中声明,要么在intent中添加flag,二者择一即可)

看下google文档

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


cet-4的基本能看懂吧~

[color=red]备注 api level 要大于 11[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值