android 清空之前task中的activity
开发过程有个场景:关闭之前所有activity,跳转到登录页
以前是activity都放到一个list里面,退出应用的时候遍历列表,逐个finish
换种实现方式
跳转登录页的intent添加两个flagIntent.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的基本能看懂吧~
备注apilevel要大于11
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30