天天看點

Android生命周期Activity lifecycleservice生命周期Broadcast receiver lifecycle程序的生命周期

在 Android 中,多數情況下每個程式都是在各自獨立的 Linux 程序中運作的。當一個程式或其某些部分被請求時,它的程序就“出生”了;當這個程式沒有必要再運作下去且系統需要回收這個程序的記憶體用于其他程式時,這個 程序就“死亡”了。可以看出,Android 程式的生命周期是由系統控制而非程式自身直接控制。這和我們編寫桌面應用程式時的思維有一些不同,一個桌面應用程式的程序也是在其他程序或使用者請求時被創 建,但是往往是在程式自身收到關閉請求後執行一個特定的動作(比如從 main 函數中 return)而導緻程序結束的。要想做好某種類型的程式或者某種平台下的程式的開發,最關鍵的就是要弄清楚這種類型的程式或整個平台下的程式的一般工作 模式并熟記在心。在 Android 中,程式的生命周期控制就是屬于這個範疇——我的個人了解:)

在 Android 系統中,當某個 activity調用 startActivity(myIntent) 時,系統會在所有已經安裝的程式中尋找其 intent filter 和 myIntent 最比對的一個 activity,啟動這個程序,并把這個 intent 通知給這個 activity。這就是一個程式的“生”。比如我們在 Home application 中選擇 “Web browser”,系統會根據這個 intent 找到并啟動 Web browser 程式,顯示 Web browser 的一個 activity 供我們浏覽網頁(這個啟動過程有點類似我們在在個人電腦上輕按兩下桌面上的一個圖示,啟動某個應用程式)。在 Android 中,所有的應用程式“生來就是平等的”,是以不光 Android 的核心程式甚至第三方程式也可以發出一個 intent 來啟動另外一個程式中的一個 activity。Android 的這種設計非常有利于“程式部件”的重用。

  一個 Android 程式的程序是何時被系統結束的呢?通俗地說,一個即将被系統關閉的程式是系統在記憶體不足(low memory)時,根據“重要性層次”選出來的“犧牲品”。一個程序的重要性是根據其中運作的部件和部件的狀态決定的。各種程序按照重要性從高到低排列如 下:

  1. 前台程序。這樣的程序擁有一個在螢幕上顯示并和使用者互動的 activity 或者它的一個IntentReciver 正在運作。這樣的程式重要性最高,隻有在系統記憶體非常低,萬不得已時才會被結束。

  2. 可見程序。在螢幕上顯示,但是不在前台的程式。比如一個前台程序以對話框的形式顯示在該程序前面。這樣的程序也很重要,它們隻有在系統沒有足夠記憶體運作所有前台程序時,才會被結束。

  3. 服務程序。這樣的程序在背景持續運作,比如背景音樂播放、背景資料上傳下載下傳等。這樣的程序對使用者來說一般很有用,是以隻有當系統沒有足夠記憶體來維持所有的前台和可見程序時,才會被結束。

  5. 空程序。這樣的程序不包含任何活動的程式部件。系統可能随時關閉這類程序。

從某種意義上講,垃圾收集機制把程式員從“記憶體管理噩夢”中解放出來,而 Android 的程序生命周期管理機制把使用者從“任務管理噩夢”中解放出來。我見過一些 Nokia S60 使用者和 Windows Mobile 使用者要麼因為長期不關閉多餘的應用程式而導緻系統變慢,要麼因為不時檢視應用程式清單而影響使用體驗。Android 使用 Java 作為應用程式 API,并且結合其獨特的生命周期管理機制同時為開發者和使用者提供最大程度的便利。

Active:處于螢幕前景(目前task的棧頂Activity處于Active狀态),同一時刻隻能有一個Activity處于Active狀态;

Paused狀态:處于背景畫面畫面狀态,失去了焦點,但依然是活動狀态;

stopped:不可見,但依然保持所有的狀态和記憶體資訊。

可以調用finish()結束處理Paused或者stopped狀态的Activity。

<code>void onCreate(Bundle savedInstanceState)</code> <code>void onStart()</code> <code>void onRestart()</code> <code>void onResume()</code> <code>void onPause()</code> <code>void onStop()</code> <code>void onDestroy()</code>
Android生命周期Activity lifecycleservice生命周期Broadcast receiver lifecycle程式的生命周期

Unlike <code>onPause()</code> and the other methods discussed earlier, <code>onSaveInstanceState()</code> and <code>onRestoreInstanceState()</code>are not lifecycle methods. They are not always called. Because <code>onSaveInstanceState()</code> is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use <code>onPause()</code> for that purpose instead.

The current activity's <code>onPause()</code> method is called.

Next, the starting activity's <code>onCreate()</code>, <code>onStart()</code>, and <code>onResume()</code> methods are called in sequence.

Then, if the starting activity is no longer visible on screen, its <code>onStop()</code> method is called.

A service can be used in two ways:

相關的方法:

<code>void onCreate()</code>

<code>void onStart(Intent intent)</code>

If a service permits others to bind to it, there are additional callback methods for it to implement:

<code>IBinder onBind(Intent intent)</code>

<code>boolean onUnbind(Intent intent)</code>

<code>void onRebind(Intent intent)</code>

Android生命周期Activity lifecycleservice生命周期Broadcast receiver lifecycle程式的生命周期

隻有一個方法:void onReceive(Context curContext, Intent broadcastMsg)

A process with an active broadcast receiver is protected from being killed. But a process with only inactive components can be killed by the system at any time, when the memory it consumes is needed by other processes.

This presents a problem when the response to a broadcast message is time consuming and, therefore, something that should be done in a separate thread, away from the main thread where other components of the user interface run. If<code>onReceive()</code> spawns the thread and then returns, the entire process, including the new thread, is judged to be inactive (unless other application components are active in the process), putting it in jeopardy of being killed. The solution to this problem is for <code>onReceive()</code> to start a service and let the service do the job, so the system knows that there is still active work being done in the process.

Android根據其重要性在記憶體不足的時候移去重要性最低的程序。重要性由高到低為:

前台程序

可見程序

服務程序

背景程序

空程序

注意:Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. 比如播放MP3的時候就要啟動一個service。

本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2010/01/01/1637427.html,如需轉載請自行聯系原作者

繼續閱讀