android之taskAffinity+allowTaskReparenting

來源:互聯網
上載者:User

標籤:

taskAffinity:指定activity的宿主對象(task)。

可以指定activity自身所需要進入的task的執行個體對象,如果未指定,預設使用application中的taskAffinity(預設為包名),注意設定taskAffinity設定的String需要帶首碼.,taskAffinity=".youname"。否則可能會報錯INSTALL_PARSE_FAILED_MANIFEST_MALFORMED。

在這個文檔裡面其實講了很多:http://developer.android.com/guide/topics/manifest/activity-element.html

這裡給示範一些執行個體,以及解決一些疑問。

 

實驗一:啟動兩個應用的launchmodetest1、launchmodetest2,launchmodetest1.MainActivity保留在預設task(launchmodetest1)中,launchmodetest2.MainActivity保留在預設task(launchmodetest2)中,而launchmodetest1.OtherActivity1+launchmodetest2.OtherActivity1保留在task(launchmodetest)中。

可以指定activity自身所需要進入的task的執行個體對象,如果未指定,預設包名。注意設定taskAffinity設定的String需要帶首碼.,taskAffinity=".youname"。否則會報錯INSTALL_PARSE_FAILED_MANIFEST_MALFORMED。

前期準備,打日誌相關。 

 1     //<uses-permission android:name="android.permission.GET_TASKS"/> 2     private final String TAG = getClass().getName(); 3     @Override 4     protected void onResume() { 5         new Handler().postDelayed(new Runnable() { 6             @Override 7             public void run() { 8                 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 9                 List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);10                 if(null != runningTaskInfos){11                     RunningTaskInfo info = runningTaskInfos.get(0);12                     Log.e(TAG, "task id:"+info.id+" base:"+info.baseActivity.toString());13                     Log.e(TAG, "task id:"+info.id+" top:"+info.topActivity.toString());14                 }15             }16         }, 500);17         super.onResume();18     }

project:com.example.launchmodetest1/com.example.launchmodetest2。

activities:com.example.launchmodetest1.MainActivity/com.example.launchmodetest2.MainActivity

MainActivity:taskAffinity=".launchmodetest"。

project運行com.example.launchmodetest1,然後home:

01-08 22:26:43.915: E/com.example.launchmodetest1.MainActivity(27622): task id:302 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.MainActivity}
01-08 22:26:43.915: E/com.example.launchmodetest1.MainActivity(27622): task id:302 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.MainActivity}

project運行com.example.launchmodetest2:

01-08 22:27:11.695: E/com.example.launchmodetest2.MainActivity(27810): task id:302 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.MainActivity}
01-08 22:27:11.695: E/com.example.launchmodetest2.MainActivity(27810): task id:302 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}

OK,launchmodetest2進入launchmodetest1棧中,但是關閉應用後重新再啟動時,啟動的都是launchmodetest1,結果和之前不一樣。環境不是很純粹,adb啟動activity會帶參數Intent.FLAG_ACTIVITY_NEW_TASK,導致結果有出入。

新增activities:com.example.launchmodetest1.OtherActivity1/com.example.launchmodetest2.OtherActivity1,MainActivity啟動OtherActivity1,OtherActivity1:taskAffinity=".launchmodetest"。

使用startActivity(new Intent("com.example.launchmodetest1.OtherActivity1"),startActivity(new Intent("com.example.launchmodetest1.OtherActivity1"),測試不通過。

 

啟動:startActivity(new Intent("com.example.launchmodetest1.OtherActivity1").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)),startActivity(new Intent("com.example.launchmodetest1.OtherActivity1").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))。

01-08 23:13:43.295: E/com.example.launchmodetest1.MainActivity(1495): task id:338 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.MainActivity}
01-08 23:13:43.295: E/com.example.launchmodetest1.MainActivity(1495): task id:338 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.MainActivity}
01-08 23:13:44.795: E/com.example.launchmodetest1.OtherActivity1(1495): task id:341 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-08 23:13:44.795: E/com.example.launchmodetest1.OtherActivity1(1495): task id:341 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-08 23:13:50.485: E/com.example.launchmodetest2.MainActivity(1328): task id:342 base:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-08 23:13:50.485: E/com.example.launchmodetest2.MainActivity(1328): task id:342 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-08 23:13:51.895: E/com.example.launchmodetest2.OtherActivity1(1328): task id:341 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-08 23:13:51.895: E/com.example.launchmodetest2.OtherActivity1(1328): task id:341 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.OtherActivity1}

測試通過,launchmodetest1.MainActivity->launchmodetest1.OtherActivity1->home->launchmodetest2.MainActivity->launchmodetest2.OtherActivity1

 

連續返回:

01-08 23:14:42.405: E/com.example.launchmodetest1.OtherActivity1(1495): task id:341 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-08 23:14:42.405: E/com.example.launchmodetest1.OtherActivity1(1495): task id:341 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-08 23:14:44.015: E/com.example.launchmodetest2.MainActivity(1328): task id:342 base:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-08 23:14:44.015: E/com.example.launchmodetest2.MainActivity(1328): task id:342 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}

結果:launchmodetest2.OtherActivity1->launchmodetest1.OtherActivity1->launchmodetest2.MainActivity,符合預期。

解釋:launchmodetest1.MainActivity(task(launchmodetest1))->launchmodetest1.OtherActivity1(task(launchmodetest))->home((task(launcher)))->launchmodetest2.MainActivity((task(launchmodetest2)))->launchmodetest2.OtherActivity1((task(launchmodetest))),啟動結束時,涉及4個task,task(launchmodetest1)在task(launcher)之下,task(launcher)之上有兩個task,task(launchmodetest2)+task(launchmodetest),當前顯示的是task(launchmodetest).aunchmodetest2.OtherActivity1,點擊返回,回到task(launchmodetest).launchmodetest1.OtherActivity1,再返回,回到task(launchmodetest2),顯示task(launchmodetest2).launchmodetest2.MainActivity,再返回,回到task(launcher)。

總結:taskAffinity+Intent.FLAG_ACTIVITY_NEW_TASK在第一次選擇task時可以宿主到taskAffinity設定的task裡。

 

allowTaskReparenting:允許activity的重新宿主對象(task)。

如果一個應用的activity1啟動另一個應用的activity2(standard)且未使用Intent.FLAG_ACTIVITY_NEW_TASK,那麼activity2有可能在該task裡面完成自己的生命週期,回不到它原taskAffinity指定的task裡面。

實驗一:activity重新宿主

project:com.example.launchmodetest1/com.example.launchmodetest2。

activities:com.example.launchmodetest1.MainActivity、com.example.launchmodetest1.OtherActivity1/com.example.launchmodetest2.MainActivity

 

OtherActivity1:allowTaskReparenting="true"。

啟動launchmodetest2.MainActivity->launchmodetest1.OtherActivity1

 

01-09 01:40:15.595: E/com.example.launchmodetest2.MainActivity(13210): task id:429 base:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-09 01:40:15.595: E/com.example.launchmodetest2.MainActivity(13210): task id:429 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-09 01:40:19.205: E/com.example.launchmodetest1.OtherActivity1(13080): task id:429 base:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-09 01:40:19.205: E/com.example.launchmodetest1.OtherActivity1(13080): task id:429 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}

home->launchmodetest2

01-09 01:42:15.055: E/com.example.launchmodetest2.MainActivity(13210): task id:429 base:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}
01-09 01:42:15.055: E/com.example.launchmodetest2.MainActivity(13210): task id:429 top:ComponentInfo{com.example.launchmodetest2/com.example.launchmodetest2.MainActivity}

home->launchmodetest1

01-09 01:42:42.515: E/com.example.launchmodetest1.OtherActivity1(13080): task id:431 base:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}
01-09 01:42:42.515: E/com.example.launchmodetest1.OtherActivity1(13080): task id:431 top:ComponentInfo{com.example.launchmodetest1/com.example.launchmodetest1.OtherActivity1}

解釋:launchmodetest2.MainActivity(task(launchmodetest2))->launchmodetest1.OtherActivity1(task(launchmodetest2))->launcher(task(launcher))->launchmodetest2(task(launchmodetest2))->launcher->launchmodetest1(task(launchmodetest1))此時launchmodetest1.OtherActivity1的task從(task(launchmodetest2))->(task(launchmodetest1))。

 

android之taskAffinity+allowTaskReparenting

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.