allowTaskReparenting的主要作用是activity的迁移,即从一个task迁移到另一个task,这个迁移跟activity的taskAffinity有关,必须是从一个跟该activity taskAffinity不同的task中迁移到跟它taskAffinity相同的task中,且看了下android源代码,只有在发生rese ttask时才会去使用这个标志,而我们从home上启动的程序都带了FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。所以从home上启动程序会进行task reset.也就会使用到这个allowTaskReparenting。
下面来进行我们的测试:
有两个apk:
apk1中含有三个Activity:MainActivity,ActivityB,ActivityB设置了allowTaskReparenting为false.
apk2中含有一个activity:MainActivityB,MainActivityB点击的时候会启动ActivityB
首先我们启动apk2,进入MainActivityB,点击MainActivityB会进入ActivityB,我们看下 这个时候的堆栈:
- ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
- Main stack:
- TaskRecord{419878b0 #82 A com.example.testactivitylaunchflag2 U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- Hist #3: ActivityRecord{413c99d8 u0 com.leaves.ipanel/.ActivityB}
- Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
- ProcessRecord{413cba38 15618:com.leaves.ipanel/u0a10061}
- Hist #2: ActivityRecord{41984600 u0 com.example.testactivitylaunchflag2/.MainActivityB}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- ProcessRecord{413bccb8 15600:com.example.testactivitylaunchflag2/u0a10063}
- 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 #1: 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}
- TaskRecord{413cdd58 #69 A com.android.systemui U 0}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity (has extras) }
- Hist #0: ActivityRecord{41429718 u0 com.android.systemui/.recent.RecentsActivity}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity bnds=[550,3495][862,3690] (has extras) }
- ProcessRecord{414125b0 464:com.android.systemui/u0a10012}
- ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
- Main stack:
- 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 #3: 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}
- TaskRecord{419878b0 #82 A com.example.testactivitylaunchflag2 U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- Hist #2: ActivityRecord{413c99d8 u0 com.leaves.ipanel/.ActivityB}
- Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
- ProcessRecord{413cba38 15618:com.leaves.ipanel/u0a10061}
- Hist #1: ActivityRecord{41984600 u0 com.example.testactivitylaunchflag2/.MainActivityB}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- ProcessRecord{413bccb8 15600:com.example.testactivitylaunchflag2/u0a10063}
- TaskRecord{413cdd58 #69 A com.android.systemui U 0}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity (has extras) }
- Hist #0: ActivityRecord{41429718 u0 com.android.systemui/.recent.RecentsActivity}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity bnds=[550,3495][862,3690] (has extras) }
- ProcessRecord{414125b0 464:com.android.systemui/u0a10012}
在从Home启动apk1:
- ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
- Main stack:
- TaskRecord{423314b8 #83 A com.leaves.ipanel U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.leaves.ipanel/.MainActivity bnds=[63,1124][207,1268] }
- Hist #4: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.leaves.ipanel/.MainActivity bnds=[63,1124][207,1268] }
- ProcessRecord{413cba38 15618: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 #3: 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}
- TaskRecord{419878b0 #82 A com.example.testactivitylaunchflag2 U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- Hist #2: ActivityRecord{413c99d8 u0 com.leaves.ipanel/.ActivityB}
- Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
- ProcessRecord{413cba38 15618:com.leaves.ipanel/u0a10061}
- Hist #1: ActivityRecord{41984600 u0 com.example.testactivitylaunchflag2/.MainActivityB}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- ProcessRecord{413bccb8 15600:com.example.testactivitylaunchflag2/u0a10063}
- TaskRecord{413cdd58 #69 A com.android.systemui U 0}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity (has extras) }
- Hist #0: ActivityRecord{41429718 u0 com.android.systemui/.recent.RecentsActivity}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity bnds=[550,3495][862,3690] (has extras) }
- ProcessRecord{414125b0 464:com.android.systemui/u0a10012}
我们把ActivityB的allowTaskReparenting改为true,这个时候前面的步骤一样,当我们从Home启动apk2时,我们惊奇的发现,启动的竟然是ActivityB,可以看到此时的堆栈:
- ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
- Main stack:
- TaskRecord{41802828 #86 A com.leaves.ipanel U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.leaves.ipanel/.MainActivity }
- Hist #4: ActivityRecord{41840a38 u0 com.leaves.ipanel/.ActivityB}
- Intent { act=com.leaves.ipanel.ActivityB cmp=com.leaves.ipanel/.ActivityB }
- ProcessRecord{431e8a80 15785:com.leaves.ipanel/u0a10061}
- Hist #3: ActivityRecord{41802640 u0 com.leaves.ipanel/.MainActivity}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.leaves.ipanel/.MainActivity }
- 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 #2: 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}
- TaskRecord{416dd178 #85 A com.example.testactivitylaunchflag2 U 0}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- Hist #1: ActivityRecord{42401f10 u0 com.example.testactivitylaunchflag2/.MainActivityB}
- Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.testactivitylaunchflag2/.MainActivityB bnds=[435,1124][579,1268] }
- ProcessRecord{4140d318 15767:com.example.testactivitylaunchflag2/u0a10063}
- TaskRecord{413cdd58 #69 A com.android.systemui U 0}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity (has extras) }
- Hist #0: ActivityRecord{41429718 u0 com.android.systemui/.recent.RecentsActivity}
- Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity bnds=[550,3495][862,3690] (has extras) }
- ProcessRecord{414125b0 464:com.android.systemui/u0a10012}