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

主要代码:

        
              
                  
                  
              
              


MainActivity启动activity
    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");      
        
            startActivity(intent);   
        }  


启动A之后的堆栈:
    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}  


可见,跟启动他的activity同属于一个task.

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);   
    }  


启动A之后的堆栈
    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}  

可见,跟启动他的activity还是同属于一个task.

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


    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");      
          
        startActivity(intent);   
    }  


        
               
                   
                   
               
               

查看一下启动A之后的堆栈:
    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添加上去


    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);   
    }  

查看一下启动A之后的堆栈:

    ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)  
      Main stack:  
        TaskRecord{4140d4c8 #42 A com.leaves.test.ActivityA U 0}  
        Intent { act=com.leaves.ipanel.ActivityA flg=0x10000000 cmp=com.leaves.ipanel/.ActivityA }  
          Hist #2: ActivityRecord{416b48d8 u0 com.leaves.ipanel/.ActivityA}  
            Intent { act=com.leaves.ipanel.ActivityA flg=0x10000000 cmp=com.leaves.ipanel/.ActivityA }  
            ProcessRecord{41393ed0 8028:com.leaves.ipanel/u0a10061}  
        TaskRecord{4231cf40 #41 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 #1: ActivityRecord{413c8430 u0 com.leaves.ipanel/.MainActivity}  
            Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }  
            ProcessRecord{41393ed0 8028: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}  

可以看到,这次终于在一个新的task中启动了ActivityA.

上面我们测试的两个Activity是在同一个应用中,如果他们在不同的应用中呢,例如ActivityA跟MainActivity不在一个apk中,其实情况还是一样的,只是这个时候,如果没有在AndroidManifest.xml中设置taskAffinity,则他们两个的taskAffinity是不一样的。因为默认如果没有设置taskAffinity,则taskAffinity跟包名是一样的,所以,如果ActivityA跟MainActivity不在一个apk中,当没有FLAG_ACTIVITY_NEW_TASK时,则两者应该是在同一个task,但是如果有FLAG_ACTIVITY_NEW_TASK,则两者应该会在不同的task中。


总结:

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

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

你可能感兴趣的:(Android,进阶,taskAffinity,Andorid)