Android之 FLAG_ACTIVITY_CLEAR_TASK
【摘要】 FLAG_ACTIVITY_CLEAR_TASK :如果在调用Context.startActivity时传递这个标记,将会导致任何用来放置该activity的已经存在的task里面的已经存在
的activity先清空,然后该activity再在该task中启动,也就是说,这个新启动的activity变为了这个空tas的根activity.所有老的activity都结束...
FLAG_ACTIVITY_CLEAR_TASK :如果在调用Context.startActivity时传递这个标记,将会导致任何用来放置该activity的已经存在的task里面的已经存在
的activity先清空,然后该activity再在该task中启动,也就是说,这个新启动的activity变为了这个空tas的根activity.所有老的activity都结束掉。
该标志必须和FLAG_ACTIVITY_NEW_TASK一起使用。
我们有三个activity:MainActivity、ActivityA、ActivityB. 依次从MainActivity中启动ActivityA,从ActivityA中启动ActivityB.
首先我们所有的都使用缺省的,也即没有设置taskAffinity、launchMode,启动activity时Intent也不带标记,我们看一下启动B之后的堆栈:
-
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
-
Main stack:
-
TaskRecord{4199dd68 #56 A com.leaves.ipanel U 0}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
-
Hist #3: ActivityRecord{4140fdf8 u0 com.leaves.ipanel/.ActivityB}
-
Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
-
ProcessRecord{41517e80 13350:com.leaves.ipanel/u0a10061}
-
Hist #2: ActivityRecord{413ff7e8 u0 com.leaves.ipanel/.ActivityA}
-
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
-
ProcessRecord{41517e80 13350:com.leaves.ipanel/u0a10061}
-
Hist #1: ActivityRecord{413f8b28 u0 com.leaves.ipanel/.MainActivity}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
-
ProcessRecord{41517e80 13350:com.leaves.ipanel/u0a10061}
-
TaskRecord{41350f60 #2 A com.android.launcher U 0}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/com.android.launcher2.Launcher }
-
Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
-
ProcessRecord{41615818 628:com.android.launcher/1000}
可以看到三者在同一个task中,且都存在。
接下来我们在启动ActivityB时添加FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK
-
public void onClick(View v) {
-
// TODO Auto-generated method stub
-
Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
-
-
Intent intent = new Intent("com.leaves.ipanel.ActivityB");
-
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
-
startActivity(intent);
-
}
再看下启动ActivityB之后的堆栈:
-
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
-
Main stack:
-
TaskRecord{4245bd10 #57 A com.leaves.ipanel U 0}
-
Intent { act=com.leaves.ipanel.ActivityB flg=0x10008000 cmp=com.leaves.ipanel/.ActivityB }
-
Hist #1: ActivityRecord{413f8b28 u0 com.leaves.ipanel/.ActivityB}
-
Intent { act=com.leaves.ipanel.ActivityB flg=0x10008000 cmp=com.leaves.ipanel/.ActivityB }
-
ProcessRecord{41413010 13441:com.leaves.ipanel/u0a10061}
-
TaskRecord{41350f60 #2 A com.android.launcher U 0}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/com.android.launcher2.Launcher }
-
Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
-
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
-
ProcessRecord{41615818 628:com.android.launcher/1000}
可以看到,MainActivity和ActivityA都被结束掉了,原因就是因为该task用来装载ActivityB,而启动时又设置了FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/51786525
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)