Andorid之taskAffinity 和 FLAG_ACTIVITY_NEW_TASK
taskAffinity 和 FLAG_ACTIVITY_NEW_TASK都是和activity啟動(dòng)時(shí)是否需要新建一個(gè)task有關(guān),我們分四種情況看一下這兩個(gè)標(biāo)志對(duì)啟動(dòng)activity的影響:(前提:從MainActivity中啟動(dòng)ActivityA)
1)、兩個(gè)標(biāo)志都不設(shè)置
2)、有FLAG_ACTIVITY_NEW_TASK ?
3)、無FLAG_ACTIVITY_NEW_TASK有taskAffinity (不同于MainActivity)
4)、有FLAG_ACTIVITY_NEW_TASK有taskAffinity
注意上面的標(biāo)志都是針對(duì)于啟動(dòng)的ActivityA,FLAG_ACTIVITY_NEW_TASK ?是在啟動(dòng)ActivityA的Intent中設(shè)置的,taskAffinity 是在AndroidManifest中ActivityA中設(shè)置,另外注意這里兩個(gè)actiity的啟動(dòng)模式都設(shè)置為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啟動(dòng)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); }
啟動(dòng)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}
可見,跟啟動(dòng)他的activity同屬于一個(gè)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); }啟動(dòng)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}
可見,跟啟動(dòng)他的activity還是同屬于一個(gè)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>
查看一下啟動(dòng)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); }
查看一下啟動(dòng)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}
可以看到,這次終于在一個(gè)新的task中啟動(dòng)了ActivityA.
上面我們測試的兩個(gè)Activity是在同一個(gè)應(yīng)用中,如果他們?cè)诓煌膽?yīng)用中呢,例如ActivityA跟MainActivity不在一個(gè)apk中,其實(shí)情況還是一樣的,只是這個(gè)時(shí)候,如果沒有在AndroidManifest.xml中設(shè)置taskAffinity,則他們兩個(gè)的taskAffinity是不一樣的。因?yàn)槟J(rèn)如果沒有設(shè)置taskAffinity,則taskAffinity跟包名是一樣的,所以,如果ActivityA跟MainActivity不在一個(gè)apk中,當(dāng)沒有FLAG_ACTIVITY_NEW_TASK時(shí),則兩者應(yīng)該是在同一個(gè)task,但是如果有FLAG_ACTIVITY_NEW_TASK,則兩者應(yīng)該會(huì)在不同的task中。
總結(jié):
如果沒有設(shè)置FLAG_ACTIVITY_NEW_TASK,且是從一個(gè)activity中啟動(dòng)另一個(gè)activity,即sourceRecord不為null,則設(shè)置新啟動(dòng)的TaskRecord為即sourceRecord的TaskRecord
如果設(shè)置了FLAG_ACTIVITY_NEW_TASK,則根據(jù)task的 affinity、intent、ComponentName等查找是否有合適的TaskRecord
總結(jié)
以上是生活随笔為你收集整理的Andorid之taskAffinity 和 FLAG_ACTIVITY_NEW_TASK的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android之adb shell d
- 下一篇: Android之用Intent.FLAG