初學 Android 架構元件之 Lifecycle
在開發應用時,我們可能會基於一系列的生命週期實現某種功能。為了複用,也為了不讓應用元件變得很臃腫,實現該功能時會選擇與生命週期元件解藕,獨立成一種元件。這樣能夠很方便地在應用元件中使用,比如: Activity
、 Fragment
或 Service
。
Android 官方把它叫做 lifecycle-aware 元件,這類元件能夠感知應用元件生命週期的變化,避免一連串顯性的生命週期方法委託呼叫。雖然可以建立基類,在基類中進行委託呼叫,但由於 Java 是單繼承的,就會存在一定的限制。不然發現,這是一次組合優於繼承的實踐。
為了方便開發者建立 lifecycle-aware 元件, androidx.lifecycle
包提供了一些類與介面。
Lifecycle
Lifecycle
類表示Android應用元件的生命週期,是被觀察者。這是一個抽象類,它的實現是 LifecycleRegistry
類。另外,它通過使用兩類資料來跟蹤應用元件的生命週期變化,一種是事件,另一種是狀態。
Lifecycle.Event
表示生命週期的事件,與應用元件的生命週期回撥一一對應,這些事件分別是: ON_CREATE
、 ON_START
、 ON_RESUME
、 ON_PAUSE
、 ON_STOP
、 ON_DESTROY
、 ON_ANY
。最後一種事件可以代表前面任意一種。
舉個例子,當 Activity
的 onCreate()
生命週期方法被呼叫時會產生 ON_CREATE
事件,觀察者可以監聽該事件以便處理 Activity
此時的生命週期。
Lifecycle.State
表示生命週期的狀態,一共有5種,分別是: INITIALIZED
、 DESTROYED
、 CREATED
、 STARTED
、 RESUMED
。
應用元件初始化之後進入 INITIALIZED
狀態,在 onCreate()
生命週期方法呼叫後進入 CREATED
狀態,在 onStart()
生命週期方法呼叫後進入 STARTED
狀態,在 onResume()
生命週期方法呼叫後進入 RESUMED
狀態。
事件與狀態之間的具體變化關係如下圖所示:

Lifecycle
物件有3個方法:
void addObserver(LifecycleObserver observer) void removeObserver(LifecycleObserver observer) State getCurrentState()
LifecycleOwner
LifecycleOwner
介面表示生命週期所有者,即擁有生命週期的應用元件。通過呼叫 getLifecycle()
方法能夠獲得它所擁有的 Lifecycle
物件。
FragmentActivity
和 Fragment
均已實現該介面,可以直接使用。當然開發者也可以自定義。 LifecycleService
和 ProcessLifecycleOwner
是另外兩個內建的實現類。
LifecycleObserver
標記介面 LifecycleObserver
表示生命週期觀察者,是 lifecycle-aware 元件。
小試牛刀
新增依賴
新建一個 Android Studio 專案,在 build.gradle
檔案裡新增 google()
倉庫。
allprojects { repositories { google() jcenter() } } 複製程式碼
在 app 模組的 build.gradle
檔案裡新增依賴。
dependencies { def version = "2.0.0-alpha1" implementation "androidx.lifecycle:lifecycle-runtime:$version" annotationProcessor "androidx.lifecycle:lifecycle-compiler:$version" } 複製程式碼
例子1:列印生命週期方法被呼叫日誌
實現觀察者。
class MyObserver implements LifecycleObserver { private static final String TAG = MyObserver.class.getSimpleName(); @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) public void onCreate() { Log.d(TAG, "onCreate called"); } @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { Log.d(TAG, "onStart called"); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume() { Log.d(TAG, "onResume called"); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void onPause() { Log.d(TAG, "onPause called"); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void onStop() { Log.d(TAG, "onStop called"); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void onDestroy() { Log.d(TAG, "onDestroy called"); } @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void onAny() { Log.d(TAG, "onCreate | onStart | onResume | onPause | onStop | onDestroy called"); } } 複製程式碼
在 Activity
中使用該觀察者。
public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // ... getLifecycle().addObserver(new MyObserver()); } } 複製程式碼
例子2:網路變化觀察者
實現這樣一個 lifecycle-aware 元件,它能夠:在 onCreate()
生命週期中動態註冊網路變化 BroadcastReceiver
,在 onDestory()
生命週期中登出廣播接收者,在收到網路變化時能夠判斷出網路是如何變化的,並通過回撥告知使用者。
實現觀察者。
package com.samelody.samples.lifecycle import android.content.BroadcastReceiver import android.content.Context import android.content.Context.CONNECTIVITY_SERVICE import android.content.Intent import android.content.IntentFilter import android.net.ConnectivityManager import android.net.ConnectivityManager.CONNECTIVITY_ACTION import android.net.ConnectivityManager.EXTRA_NETWORK_TYPE import androidx.lifecycle.Lifecycle.Event.ON_START import androidx.lifecycle.Lifecycle.Event.ON_STOP import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.OnLifecycleEvent /** * The network observer. * * @author Belin Wu */ class NetworkObserver(private val context: Context) : LifecycleObserver { /** * The network receiver. */ private val receiver = NetworkReceiver() /** * The last type of network. */ private var lastType = TYPE_NONE /** * The network type changed listener. */ var listener: OnNetworkChangedListener? = null @OnLifecycleEvent(ON_START) fun onStart() { val filter = IntentFilter() filter.addAction(CONNECTIVITY_ACTION) context.registerReceiver(receiver, filter) } @OnLifecycleEvent(ON_STOP) fun onStop() { context.unregisterReceiver(receiver) } companion object { /** * The network type: None. */ const val TYPE_NONE = -1 /** * The network type: Mobile. */ const val TYPE_MOBILE = 0 /** * The network type: Wi-Fi. */ const val TYPE_WIFI = 1 } /** * The network receiver. */ inner class NetworkReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val manager = context?.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager val oldType = intent?.getIntExtra(EXTRA_NETWORK_TYPE, TYPE_NONE) var newType = manager.activeNetworkInfo.type newType = when { oldType == TYPE_MOBILE && newType == TYPE_WIFI -> TYPE_NONE oldType == TYPE_WIFI && newType == TYPE_MOBILE -> TYPE_NONE else -> newType } if (lastType == newType) { return } listener?.invoke(lastType, newType) } } } 複製程式碼
定義網路變化監聽器。
package com.samelody.samples.lifecycle /** * The network type changed listener. Called when the network type is changed. * * @author Belin Wu */ typealias OnNetworkChangedListener = (Int, Int) -> Unit 複製程式碼
在 Activity
中使用該觀察者。
package com.samelody.samples.lifecycle import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity /** * The sample activity. * * @author Belin Wu */ class SampleActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val observer = NetworkObserver(this) observer.listener = { from: Int, to: Int -> Log.d("Sample", "The network is changed from $from to $to") } lifecycle.addObserver(observer) } } 複製程式碼
在 Service
中使用該觀察者。
package com.samelody.samples.lifecycle import android.util.Log import androidx.lifecycle.LifecycleService /** * The sample service. * * @author Belin Wu */ class SampleService : LifecycleService() { override fun onCreate() { super.onCreate() val observer = NetworkObserver(this) observer.listener = { from: Int, to: Int -> Log.d("Sample", "The network is changed from $from to $to") } lifecycle.addObserver(observer) } } 複製程式碼