flag_activity_clear_top
flag_activity_clear_top :如果设置,并且要启动的activity已经在当前的任务中,那么在该activity之上的activity都会关闭,并且intent会传递给老的activity(现在在栈顶)
例如,假设一个任务包含四个activity:A,B,C,D,假设D中启动B并且带有次标志,则 C和D都会结束,并且B会收到intet,现在的栈变成了A,B
在上面的例子中,当前运行的B要么在他的onNewIntent方法中接受这个Intent,或者结束掉已经存在的实例并且重启一个,这依赖与B的启动模式,如果是确实的"multiple(即standard)",那么结束掉实例并重启一个,其他启动模式下将会调用他的onNewIntent
示例:
假设从MainActivity启动ActivityA,从ActivityA中启动ActivityB,在从ActivityB中启动ActivityA且A的启动模式为standard,其他都为默认,
1、不设置,我们看一下按照上面这个顺序启动后的堆栈:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Main stack:
TaskRecord{4240b790 #60 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 #4: ActivityRecord{413e6990 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41344a80 13731:com.leaves.ipanel/u0a10061}
Hist #3: ActivityRecord{413c8430 u0 com.leaves.ipanel/.ActivityB}
Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
ProcessRecord{41344a80 13731:com.leaves.ipanel/u0a10061}
Hist #2: ActivityRecord{4133b580 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41344a80 13731:com.leaves.ipanel/u0a10061}
Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
ProcessRecord{41344a80 13731: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}
可以看到这个是我们正常结果。
2、接下来我们在ActivityB启动ActivityA的时候携带FLAG_ACTIVITY_CLEAR_TOP ,wield了比较,我们先启动到AcitvityB,看一下堆栈:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Main stack:
TaskRecord{42f58468 #61 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{41388e48 u0 com.leaves.ipanel/.ActivityB}
Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
ProcessRecord{41343690 13827:com.leaves.ipanel/u0a10061}
Hist #2: ActivityRecord{4133b580 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41343690 13827:com.leaves.ipanel/u0a10061}
Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
ProcessRecord{41343690 13827: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里面有三个activity,我们在从ActivityB中启动ActivityA,
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
Intent intent = new Intent("com.leaves.ipanel.ActivityA");
//Intent intent = new Intent("com.example.testactivitylaunchflag2.ActivityAA");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
//intent.addFlags(Intent.flag_activity_new_task);
startActivity(intent);
}
启动后的堆栈:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Main stack:
TaskRecord{42f58468 #61 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 #2: ActivityRecord{413b5728 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA flg=0x4000000 cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41343690 13827:com.leaves.ipanel/u0a10061}
Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
ProcessRecord{41343690 13827: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}
可以看到刚才ActivityA上面的ActivityB已经结束掉了,自己也结束掉了然后重新创建了一个,从哪里可以看出来是重新创建了呢?可以看打印出来的ActivityRecord不一样了。这是由于ActivityA的启动模式为standard,即允许有多个实例,我们把ActivityA的启动模式改为singleTask,在跑一下上面的测试流程:
启动到ActivityB后:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Main stack:
TaskRecord{414100e8 #62 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{413c8430 u0 com.leaves.ipanel/.ActivityB}
Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
ProcessRecord{41344a80 13948:com.leaves.ipanel/u0a10061}
Hist #2: ActivityRecord{4133b580 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41344a80 13948:com.leaves.ipanel/u0a10061}
Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
ProcessRecord{41344a80 13948: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}
再启动ActivityA:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Main stack:
TaskRecord{414100e8 #62 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 #2: ActivityRecord{4133b580 u0 com.leaves.ipanel/.ActivityA}
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
ProcessRecord{41344a80 13948:com.leaves.ipanel/u0a10061}
Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
ProcessRecord{41344a80 13948: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}
可以看出,ActiivtyB还是结束掉了,但ActivityA没有重新创建,我们可以看到ActivityA的ActivityRecord是一样的。这个时候调用的是ActivityA的onNewIntent,而没有重新onCreate创建一个,我们从log也可以看出来
I/ActivityB(14013): --onpause--task id = 63
I/ActivityA(14013): --onNewIntent--task id = 63
I/ActivityA(14013): --onRestart--task id = 63
I/ActivityA(14013): --onStart--task id = 63
I/ActivityA(14013): --onResume--task id = 63
D/hwcomposer( 111): fb1 realy close!
D/hwcomposer( 111): fb1 open! I/ActivityB(14013): --onStop--task id = 63
I/ActivityB(14013): --onDestroy--task id = 63
从Log可以看出调用了ActivityA的onNewIntent,而ActivityB调用了onDestroy.
使用其他的启动模式,都会调用onNewInten,而在standard模式下则会new 重新new一个
相关阅读
FLAG_ACTIVITY_CLEAR_TOP与FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TOP与FLAG_ACTIVITY_SINGLE_TOP 如果一个应用的栈自底向上是Fisrt—Second—Third 此时,当Third去调用First
Android 关闭多个视图Intent.FLAG_ACTIVITY_CLEAR_TOP
如果已经启动了四个Activity:A,B,C和D。在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的int