Android應用程式組件Activity的"singleTask"(12)

來源:互聯網
上載者:User

注意,這裡我們設定MainActivity的taskAffinity屬性值為"shy.luo.task.main.activity",

設定 SubActivity的taskAffinity屬性值為"shy.luo.task.sub.activity"。

重新編譯一下程式,在模擬器上把這 個應用程式再次跑起來,

用“adb shell dumpsys activity”命令再來查看一下系統啟動並執行的任務,就會看到:

  1. [html] view plaincopyRunning activities (most recent first): 
  2.  
  3. TaskRecord{4069c020 #4 A shy.luo.task.sub.activity} 
  4.  
  5. Run #2: HistoryRecord{40725040 shy.luo.task/.SubActivity} 
  6.  
  7. TaskRecord{40695220 #3 A shy.luo.task.main.activity} 
  8.  
  9. Run #1: HistoryRecord{406b26b8 shy.luo.task/.MainActivity} 
  10.  
  11. TaskRecord{40599c90 #2 A com.android.launcher} 
  12.  
  13. Run #0: HistoryRecord{40646628 com.android.launcher/com.android.launcher2.Launcher} 

這裡就可以看到,SubActivity和MainActivity就分別運行在不同的任務中了。

至此,我們總結一下,設定了"singleTask"啟動模式的Activity的特點:

1. 設定了"singleTask"啟動模式的Activity,它在啟動的時候,會先在系統中尋找屬性值affinity等於它的屬性值 taskAffinity的任務存在;如果存在這樣的任務,它就會在這個任務中啟動,否則就會在新任務中啟動。因此,如果我們想要設定 了"singleTask"啟動模式的Activity在新的任務中啟動,就要為它設定一個獨立的taskAffinity屬性值。

2. 如果設定了"singleTask"啟動模式的Activity不是在新的任務中啟動時,它會在已有的任務中查看是否已經存在相應的Activity實 例,如果存在,就會把位於這個Activity執行個體上面的Activity全部結束掉,即最終這個Activity執行個體會位於任務的堆棧頂端中。

看來,要解開Activity的"singleTask"之謎,還是要自力更生啊,不過,如果我們仔細閱讀官方文檔,在http://developer.android.com/guide/topics/manifest/activity-element.html中,有這樣的描述:

As shown in the table above, standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance —are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.

Regardless of the launch mode that you choose, make sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks using the BACK key.

這樣看,官方文檔也沒有坑我們呢,它告誡我們:make sure to test the usability of the activity during launch。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.