在開發應用時,咱們可能會基於一系列的生命週期實現某種功能。爲了複用,也爲了避免讓應用組件變得很臃腫,實現該功能時會選擇與生命週期組件解藕,獨立成一種組件。這樣可以很方便地在應用組件中使用,好比:Activity
、Fragment
或 Service
。java
Android 官方把它叫作 lifecycle-aware 組件,這類組件可以感知應用組件生命週期的變化,避免一連串顯性的生命週期方法委託調用。雖然能夠建立基類,在基類中進行委託調用,但因爲 Java 是單繼承的,就會存在必定的限制。否則發現,這是一次組合優於繼承的實踐。android
爲了方便開發者建立 lifecycle-aware 組件,androidx.lifecycle
包提供了一些類與接口。網絡
Lifecycle
類表示Android應用組件的生命週期,是被觀察者。這是一個抽象類,它的實現是 LifecycleRegistry
類。另外,它經過使用兩類數據來跟蹤應用組件的生命週期變化,一種是事件,另外一種是狀態。app
Lifecycle.Event
表示生命週期的事件,與應用組件的生命週期回調一一對應,這些事件分別是:ON_CREATE
、ON_START
、ON_RESUME
、ON_PAUSE
、ON_STOP
、ON_DESTROY
、ON_ANY
。最後一種事件能夠表明前面任意一種。ide
舉個例子,當 Activity
的 onCreate()
生命週期方法被調用時會產生 ON_CREATE
事件,觀察者能夠監聽該事件以便處理Activity
此時的生命週期。gradle
Lifecycle.State
表示生命週期的狀態,一共有5種,分別是:INITIALIZED
、 DESTROYED
、CREATED
、STARTED
、RESUMED
。ui
應用組件初始化以後進入 INITIALIZED
狀態,在 onCreate()
生命週期方法調用後進入 CREATED
狀態,在 onStart()
生命週期方法調用後進入 STARTED
狀態,在 onResume()
生命週期方法調用後進入 RESUMED
狀態。this
事件與狀態之間的具體變化關係以下圖所示:google
Lifecycle
對象有3個方法:spa
void addObserver(LifecycleObserver observer)
。void removeObserver(LifecycleObserver observer)
。State getCurrentState()
。LifecycleOwner
接口表示生命週期全部者,即擁有生命週期的應用組件。經過調用 getLifecycle()
方法可以得到它所擁有的Lifecycle
對象。
FragmentActivity
和 Fragment
均已實現該接口,能夠直接使用。固然開發者也能夠自定義。LifecycleService
和 ProcessLifecycleOwner
是另外兩個內置的實現類。
標記接口 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"
}
複製代碼
實現觀察者。
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());
}
}
複製代碼
實現這樣一個 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)
}
}
複製代碼