taskAffinityとFLAG_ACTIVITY_NEW_TASK

9280 ワード

taskAffinityとFLAG_ACTIVITY_NEW_TASKはいずれもactivity起動時にtaskを新規作成する必要があるかどうかに関係しており、この2つのフラグがactivity起動に与える影響を4つに分けて見てみましょう.(前提:MainActivityからActivity Aを起動する)
1)、両フラグとも設定しない2)、FLAGありACTIVITY_NEW_TASK 3)、FLAGなしACTIVITY_NEW_TASKにはtaskAffinity(MainActivityとは異なる)4)、FLAG_があるACTIVITY_NEW_TASKにはtaskAffinityがある
上のフラグは、起動したActivity A、FLAG_についてです.ACTIVITY_NEW_TASKはActivity Aを起動するIntentで設定され、taskAffinityはAndroidManifestでActivity Aで設定されています.また、ここでは2つのactityの起動モードがStandardに設定されていることに注意してください
1、まず第一中の状況を見ます.
主なコード:
 <activity android:name=".ActivityA"  
            android:launchMode="standard"
          android:label="@string/title_activityA">  
       <intent-filter>  
           <action android:name="com.leaves.ipanel.ActivityA"/>  
           <category android:name="android.intent.category.DEFAULT"/>  
       </intent-filter>  
        </activity> 
MainActivity activity起動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); 
	}
<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> 
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}
は前の2つの状況と同じです.
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でActivity Aを起動しました.
私たちがテストした2つのActivityは同じアプリケーションで、もし彼らが異なるアプリケーションの中にいたら、例えばActivity AとMainActivityは1つのapkの中にいないが、実際には同じ状況で、ただこの時、AndroidManifestにいなかったら.xmlでtaskAffinityを設定すると、2つのtaskAffinityは異なります.デフォルトでtaskAffinityが設定されていない場合、taskAffinityはパッケージ名と同じであるため、Activity AとMainActivityが1つのapkにない場合、FLAG_がない場合ACTIVITY_NEW_TASKの場合、両者は同じtaskになるはずですが、FLAG_があればACTIVITY_NEW_TASKは、両者が異なるtaskの中にいるはずだ.
まとめ:
FLAG_が設定されていない場合ACTIVITY_NEW_TASKは、1つのactivityから別のactivityを起動する、すなわちsourceRecordがnullでない場合、新しく起動したTaskRecordがsourceRecordであるTaskRecordがFLAG_が設定されている場合ACTIVITY_NEW_TASKは、taskのaffinity、intent、ComponentNameなどから適切なTaskRecordがあるかどうかを調べる
これらの作業の解析はまだActivity Stackです.JAvaのstartActivity UncheckedLocked関数で行います.