LiveData 是一種具有感知應(yīng)用組件(Activity、Fragment、Service)生命周期的,可觀察的數(shù)據(jù)存儲(chǔ)器類。這種感知能力可確保LiveData僅更新處于活躍生命周期狀態(tài)的應(yīng)用組件觀察者。(官網(wǎng)解釋)
LiveData的使用
先附上一張demo效果圖

在使用LiveData前,需要先添加依賴。
LiveData 的依賴,可以查看官方文檔,導(dǎo)入最新的版本。
現(xiàn)在示例使用的版本:
def lifecycle_version = "2.2.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
創(chuàng)建 LivaData 對(duì)象
LiveData是一個(gè)數(shù)據(jù)的封裝容器,可以存放包括實(shí)現(xiàn)了Collection的對(duì)象,如List。
LiveData對(duì)象通常存儲(chǔ)在ViewModel中,可以通過(guò)getter進(jìn)行訪問(wèn),通過(guò)setter修改(異步需要用postValue)。
ViewModel可以參考上一篇文章,Android Jetpack — ViewModel
class NumberViewModel : ViewModel() {
val num: MutableLiveData<Int> by lazy {
MutableLiveData<Int>(0)
}
}
添加 LivaData 監(jiān)聽(tīng)
//獲取ViewModel
mViewModel = ViewModelProvider(this).get(NumberViewModel::class.java)
//新建一個(gè)觀察者
val numObserver = Observer<Int> { num ->
mTvNum.text = num.toString()
}
//開(kāi)始監(jiān)聽(tīng)
mViewModel.num.observe(this, numObserver)
監(jiān)聽(tīng)時(shí)機(jī):通常是在Activity或Fragment的oncreate中,這樣的好處是當(dāng)組件處于STARTED狀態(tài)時(shí),就能接收到LiveData對(duì)象的最新數(shù)據(jù)。同時(shí),也能確保在onResult中的重復(fù)調(diào)用。
接收到數(shù)據(jù)更新的時(shí)機(jī):
- 數(shù)據(jù)發(fā)生更改
- 觀察者從非活躍狀態(tài)更改為活躍狀態(tài)
- 觀察者第二次從非活躍狀態(tài)更改為活躍狀態(tài),只有在自上次變?yōu)榛钴S狀態(tài)以來(lái)數(shù)據(jù)發(fā)生了更改時(shí),才會(huì)收到更新
注意:如果
LiveData對(duì)象num中設(shè)置了初始值,則在調(diào)用了observe()后,會(huì)立即掉用onChange(); 如果未設(shè)置初始值,則不會(huì)調(diào)用。
更新數(shù)據(jù)
mBtnAdd.setOnClickListener {
//java mViewModel.num.setValue(mViewModel.num.value as Int + 1)
mViewModel.num.value = mViewModel.num.value as Int + 1
}
更新LiveData數(shù)據(jù)需要調(diào)用MutableLiveData類的setValue(T)或者postValue(T)方法,LiveData類的這兩個(gè)方法是不公開(kāi)的。
而setValue(T)和postValue(T)兩者是有區(qū)別的:
setValue(T)只能在主線程調(diào)用,而postValue(T)可以在子線程調(diào)用。
/**
* Posts a task to a main thread to set the given value. So if you have a following code
* executed in the main thread:
* <pre class="prettyprint">
* liveData.postValue("a");
* liveData.setValue("b");
* </pre>
* The value "b" would be set at first and later the main thread would override it with
* the value "a".
* <p>
* If you called this method multiple times before a main thread executed a posted task, only
* the last value would be dispatched.
*
* @param value The new value
*/
protected void postValue(T value) {
boolean postTask;
synchronized (mDataLock) {
postTask = mPendingData == NOT_SET;
mPendingData = value;
}
if (!postTask) {
return;
}
ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
}
/**
* Sets the value. If there are active observers, the value will be dispatched to them.
* <p>
* This method must be called from the main thread. If you need set a value from a background
* thread, you can use {@link #postValue(Object)}
*
* @param value The new value
*/
@MainThread
protected void setValue(T value) {
assertMainThread("setValue");
mVersion++;
mData = value;
dispatchingValue(null);
}
通過(guò)源碼可以看出,當(dāng)調(diào)用postValue之后,會(huì)把任務(wù)拋到主線程中去執(zhí)行,而setValue是直接執(zhí)行。
需要注意的是,如果在主線程中執(zhí)行以下代碼:
liveData.postValue("a");
liveData.setValue("b");
會(huì)先把value設(shè)置為b,然后a覆蓋b的值。
擴(kuò)展
liveData還可以和Room、協(xié)程等一起使用,還有很多高級(jí)的用法,后續(xù)繼續(xù)記錄。