Andorid之taskAffinity 和 FLAG_ACTIVITY_NEW_TASK

举报
chenyu 发表于 2021/07/27 01:19:30 2021/07/27
【摘要】 taskAffinity 和 FLAG_ACTIVITY_NEW_TASK都是和activity启动时是否需要新建一个task有关,我们分四种情况看一下这两个标志对启动activity的影响:(前提:从MainActivity中启动ActivityA) 1)、两个标志都不设置 2)、有FLAG_ACTIVITY_NEW_TASK   3)、无FLAG_ACTIVI...

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、先看第一中情况:

主要代码:


     
  1. <activity android:name=".ActivityA"
  2. android:launchMode="standard"
  3. android:label="@string/title_activityA">
  4. <intent-filter>
  5. <action android:name="com.leaves.ipanel.ActivityA"/>
  6. <category android:name="android.intent.category.DEFAULT"/>
  7. </intent-filter>
  8. </activity>




     
  1. public void onClick(View arg0) {
  2. // TODO Auto-generated method stub
  3. Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
  4. Intent intent = new Intent("com.leaves.ipanel.ActivityA");
  5. startActivity(intent);
  6. }




     
  1. ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
  2. Main stack:
  3. TaskRecord{415ebce8 #37 A com.leaves.ipanel U 0}
  4. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  5. Hist #2: ActivityRecord{42358368 u0 com.leaves.ipanel/.ActivityA}
  6. Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
  7. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  8. Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
  9. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  10. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  11. TaskRecord{41350f60 #2 A com.android.launcher U 0}
  12. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  13. Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
  14. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  15. ProcessRecord{41615818 628:com.android.launcher/1000}


2、有FLAG_ACTIVITY_NEW_TASK  

我们添加FLAG_ACTIVITY_NEW_TASK  


   
  1. public void onClick(View arg0) {
  2. // TODO Auto-generated method stub
  3. Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
  4. Intent intent = new Intent("com.leaves.ipanel.ActivityA");
  5. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  6. startActivity(intent);
  7. }




  
  1. ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
  2. Main stack:
  3. TaskRecord{415ebce8 #37 A com.leaves.ipanel U 0}
  4. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  5. Hist #2: ActivityRecord{42358368 u0 com.leaves.ipanel/.ActivityA}
  6. Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
  7. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  8. Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
  9. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  10. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  11. TaskRecord{41350f60 #2 A com.android.launcher U 0}
  12. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  13. Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
  14. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  15. ProcessRecord{41615818 628:com.android.launcher/1000}

3、无FLAG_ACTIVITY_NEW_TASK有taskAffinity (不同于MainActivity)



     
  1. public void onClick(View arg0) {
  2. // TODO Auto-generated method stub
  3. Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
  4. Intent intent = new Intent("com.leaves.ipanel.ActivityA");
  5. startActivity(intent);
  6. }



  
  1. <activity android:name=".ActivityA"
  2. android:launchMode="standard"
  3. android:taskAffinity="com.leaves.test.ActivityA"
  4. android:label="@string/title_activityA">
  5. <intent-filter>
  6. <action android:name="com.leaves.ipanel.ActivityA"/>
  7. <category android:name="android.intent.category.DEFAULT"/>
  8. </intent-filter>
  9. </activity>


  
  1. ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
  2. Main stack:
  3. TaskRecord{415ebce8 #37 A com.leaves.ipanel U 0}
  4. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  5. Hist #2: ActivityRecord{42358368 u0 com.leaves.ipanel/.ActivityA}
  6. Intent { act=com.leaves.ipanel.ActivityA cmp=com.leaves.ipanel/.ActivityA }
  7. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  8. Hist #1: ActivityRecord{4132a3d0 u0 com.leaves.ipanel/.MainActivity}
  9. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  10. ProcessRecord{42384ad8 7591:com.leaves.ipanel/u0a10061}
  11. TaskRecord{41350f60 #2 A com.android.launcher U 0}
  12. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  13. Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
  14. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  15. ProcessRecord{41615818 628:com.android.launcher/1000}

4、有FLAG_ACTIVITY_NEW_TASK有taskAffinity

把FLAG_ACTIVITY_NEW_TASK添加上去



     
  1. public void onClick(View arg0) {
  2. // TODO Auto-generated method stub
  3. Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());
  4. Intent intent = new Intent("com.leaves.ipanel.ActivityA");
  5. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  6. startActivity(intent);
  7. }



     
  1. ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
  2. Main stack:
  3. TaskRecord{4140d4c8 #42 A com.leaves.test.ActivityA U 0}
  4. Intent { act=com.leaves.ipanel.ActivityA flg=0x10000000 cmp=com.leaves.ipanel/.ActivityA }
  5. Hist #2: ActivityRecord{416b48d8 u0 com.leaves.ipanel/.ActivityA}
  6. Intent { act=com.leaves.ipanel.ActivityA flg=0x10000000 cmp=com.leaves.ipanel/.ActivityA }
  7. ProcessRecord{41393ed0 8028:com.leaves.ipanel/u0a10061}
  8. TaskRecord{4231cf40 #41 A com.leaves.ipanel U 0}
  9. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  10. Hist #1: ActivityRecord{413c8430 u0 com.leaves.ipanel/.MainActivity}
  11. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }
  12. ProcessRecord{41393ed0 8028:com.leaves.ipanel/u0a10061}
  13. TaskRecord{41350f60 #2 A com.android.launcher U 0}
  14. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  15. Hist #0: ActivityRecord{41616790 u0 com.android.launcher/com.android.launcher2.Launcher}
  16. Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }
  17. ProcessRecord{41615818 628:com.android.launcher/1000}




总结:

如果没有设置FLAG_ACTIVITY_NEW_TASK,且是从一个activity中启动另一个activity,即sourceRecord不为null,则设置新启动的TaskRecord为即sourceRecord的TaskRecord
如果设置了FLAG_ACTIVITY_NEW_TASK,则根据task的 affinity、intent、ComponentName等查找是否有合适的TaskRecord

这些工作的解析还是在ActivityStack.JavastartActivityUncheckedLocked函数中进行的。

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/51786266

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。