- 关于Android的Intent.FLAG_ACTIVITY_CLEAR_TOP
-
A - B - C
当 B - A - B 跳转的时候,使用Intent的FLAG_ACTIVITY_CLEAR_TOP会让第一个B和第二个A,destory掉
但是当B - A - C跳转的时候不会调用B和A的destory
其实这个问题以前遇到过,今天遇到的时候又忘记了。查看API文档才发现原因,所以这里记录一下避免下次又忘记了:
public static final int FLAG_ACTIVITY_CLEAR_TOP
Added in API level 1If 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.
文档上说,如果设置这个属性,是当要启动的Activity已经存在当前Task中,才会在启动的时候销毁其他的Activity。
所以上面当A跳C的时候不满足此条件。
当然如果你想实现这个效果可以使用:
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
不过不幸的是,此方法要求最低API为11
我们有三个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 }
Proce***ecord{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 }
Proce***ecord{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 }
Proce***ecord{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 }
Proce***ecord{41615818 628:com.android.launcher/1000}
接下来我们在启动ActivityB时添加FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK
publicvoid 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);
}
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 }
Proce***ecord{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 }
Proce***ecord{41615818 628:com.android.launcher/1000}
转载于:https://blog.51cto.com/laokaddk/1340578