Android--Activity

1,Android中什麼是Task和Back Stack html

    Task能夠理解爲一個進程,啓動一個應用就回啓動一個進程,系統會爲該應用分配16MB的空間來共該應用使用。Back Stack是以堆棧的形式管理一個應用啓動的全部的Activity,每啓動一個Activity都會把新的Activity放到棧頂,若棧頂Activity銷燬(經過onBackPressed或者finish)以後,棧頂Activity下方的Activity會從新顯示,棧的機制就是後進先出(last in, fisrt out)。 android

     當一個Task中全部Activity都銷燬時,這個Task則銷燬,這個應用則關閉(除非還有一個Task啓動了此應用)。 app

     對於Android系統來講,後臺能夠運行多個Task任務,多個Task也是以堆棧行的進行管理。 spa

     先列出以下疑問,後續會逐一解答: htm

     <1>如何使一個應用的全部Activity只在一個Task中? 進程

     <2>當應用A調用應用B的時候,應用B會在應用A的Task中仍是會在新建的一個Task中? ip

     <3>Android系統會在Activity被stopped以後保存這個Activity的狀態(如輸入框中內容,勾選狀態等),當這個Activity被resume的時候,會顯示被stopped以前的數據,可是有一種特殊狀況:Android系統內存不足時,會將這個Activity被destory,這個Activity只能被create,以前的數據會出現丟失,若是想避免這種現象,能夠在onSaveInstanceState()方法保存數據,在Activity被create或resume的時候將歷史數據從新展現出來。 內存

2,Activity的運行模式(launch mode)/啓動方式: ci

     <1>manifest配置式launchMode。 element

           standard(默認值)--永久建立

           singTop--只要棧頂不是這個Activity的實例就永久建立

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent().

           singTask--若是在一個Task中已經建立了這個Activity時,系統將不會再在這Task中建立這個Activity,而是經過onNewIntent()方法從新調用這個Activity。

Note: Although the activity starts in a new task, theBack button still returns the user to the previous activity.

           singInstance--若是這個Activity已經存在,系統中全部的Task將公用這個Activity。

     <2>Intent.FLAG方式,會覆蓋配置式,優先級高於配置式。

     FLAG_ACTIVITY_NEW_TASK:若是Activity將啓動的Activity已經存在於Task中,則經過onNewIntent()直接將這個Activity移至棧頂,不然就會在當前Task建立這個Activity。等價於singTask

     FLAG_ACTIVITY_SING_TOP:等價於singTop.

     FLAG_ACTIVITActivityY_CLEAR_TOP:若是Task中這個Activity已經存在且不位於棧頂,則將棧中 位於這個Activity上方的全部Activity銷燬,經過onNewIntent()將這個Activity移至棧頂。

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".

3,Mainfest中各個標籤

Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".

    Android系統一個特殊行爲:當一個應用啓動以後,長時間不操做,系統則會銷燬這個應用所在的Task中的全部Activity除了root activity(這個activity是棧底activity仍是啓動activity有待確認),當返回這個應用時,則顯示這個root activity。

    設置root activity的屬性alwaysRetainTaskState爲true,上述特殊行爲將不會執行,即保留這個應用所在的Task中的全部Activity。

    設置root activity的屬性clearTaskOnLaunch爲true,不管離開應用多長時間,都銷燬這個應用所在的Task中的全部Activity,進入應用初始狀態,換句話說,clearTaskOnLaunch與alwaysRetainTaskState是相對的。

    設置root activity的屬性finishOnTaskLaunch爲true,finishOnTaskLaunch與clearTaskOnLaunch優勢類似,finishOnTaskLaunch應用於一個activity,而clearTaskOnLaunch應用於一個Task。

For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" .

 

 

 上述內容僅爲理論,有待實踐。

摘自:http://developer.android.com/index.html

相關文章
相關標籤/搜索