Andorid之taskAffinity 和 FLAG_ACTIVITY_NEW_TASK
taskAffinity 和 FLAG_ACTIVITY_NEW_TASK都是和activity启动时是否需要新建一个task有关,我们分四种情况看一下这两个标志对启动activity的影响:(前提:从MainActivity中启动ActivityA)
1)、两个标志都不设置
2)、有FLAG_ACTIVITY_NEW_TASK
3)、无FLAG_ACTIVITY_NEW_TASK有taskAffinity (不同于MainActivity)
4)、有FLAG_ACTIVITY_NEW_TASK有taskAffinity
注意上面的标志都是针对于启动的ActivityA,FLAG_ACTIVITY_NEW_TASK 是在启动ActivityA的Intent中设置的,taskAffinity 是在AndroidManifest中ActivityA中设置,另外注意这里两个actiity的启动模式都设置为standard
1、先看第一中情况:
主要代码:
2、有FLAG_ACTIVITY_NEW_TASK
我们添加FLAG_ACTIVITY_NEW_TASK
-
public void onClick(View arg0) {
-
// TODO Auto-generated method stub
-
Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
-
Intent intent = new Intent("com.leaves.ipanel.ActivityA");
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
startActivity(intent);
-
}
-
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
-
Main stack:
-
TaskRecord{415ebce8 #37 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{42358368 u0 com.leaves.ipanel/.ActivityA}
-
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
-
ProcessRecord{42384ad8 7591: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{42384ad8 7591: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=0x10000000 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}
3、无FLAG_ACTIVITY_NEW_TASK有taskAffinity (不同于MainActivity)
-
<activity android:name=".ActivityA"
-
android:launchMode="standard"
-
android:taskAffinity="com.leaves.test.ActivityA"
-
android:label="@string/title_activityA">
-
<intent-filter>
-
<action android:name="com.leaves.ipanel.ActivityA"/>
-
<category android:name="android.intent.category.DEFAULT"/>
-
</intent-filter>
-
</activity>
-
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
-
Main stack:
-
TaskRecord{415ebce8 #37 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{42358368 u0 com.leaves.ipanel/.ActivityA}
-
Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
-
ProcessRecord{42384ad8 7591: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{42384ad8 7591: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=0x10000000 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}
4、有FLAG_ACTIVITY_NEW_TASK有taskAffinity
把FLAG_ACTIVITY_NEW_TASK添加上去
总结:
如果没有设置FLAG_ACTIVITY_NEW_TASK,且是从一个activity中启动另一个activity,即sourceRecord不为null,则设置新启动的TaskRecord为即sourceRecord的TaskRecord
如果设置了FLAG_ACTIVITY_NEW_TASK,则根据task的 affinity、intent、ComponentName等查找是否有合适的TaskRecord
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/51786266
- 点赞
- 收藏
- 关注作者
评论(0)