Android Architecture Components是google官方提供的一套架構(gòu)組件葵第,用于設(shè)計(jì)構(gòu)建更加穩(wěn)健写妥,可測(cè)試和易維護(hù)的應(yīng)用,在Android Architecture Components中包括如下幾個(gè)組件:
-
LifeCycle
:是一個(gè)用于定義Android組件生命周期的對(duì)象; -
LifeCycleOwner
:擁有生命周期的組件所實(shí)現(xiàn)的接口演痒; -
LifeCycleObserver
:訂閱組件生命周期變化的類所實(shí)現(xiàn)的接口; -
ViewModal
:旨在以一種具有生命周期的數(shù)據(jù)存儲(chǔ)方式來(lái)管理界面相關(guān)的數(shù)據(jù)趋惨,在ViewModal
中存儲(chǔ)的數(shù)據(jù)在屏幕旋轉(zhuǎn)過(guò)程中并不會(huì)發(fā)生改變鸟顺; -
LiveData
:是一個(gè)可觀察的數(shù)據(jù)持有者,當(dāng)數(shù)據(jù)變化時(shí)通知觀察者器虾,同時(shí)也可以觀察組件生命周期變化讯嫂; -
Room
:數(shù)據(jù)庫(kù)操作的框架;
LifeCycle簡(jiǎn)介
關(guān)于LifeCycle兆沙,官網(wǎng)的說(shuō)明如下:
Defines an object that has an Android Lifecycle. Fragment and FragmentActivity classes implement LifecycleOwner interface which has the getLifecycle method to access the Lifecycle. You can also implement LifecycleOwner in your own classes.
由此可以看出LifeCycle是用來(lái)定義一個(gè)具有Android生命周期的類或者對(duì)象的接口欧芽,支持庫(kù)中的Fragment和FragmentActivity實(shí)現(xiàn)了LifeCycleOwener接口并可以通過(guò)getLifeCycle方法獲取LifeCycle對(duì)象,你也可以在你的類中自己實(shí)現(xiàn)LifeCycle接口葛圃。
ON_CREATE,ON_START,ON_RESUME事件會(huì)在LifeCycleOwner對(duì)應(yīng)的事件結(jié)束后回調(diào)千扔,ON_PAUSE_ON_STOP<ON_DESTROY會(huì)在LifeCycleOwner對(duì)應(yīng)的函數(shù)執(zhí)行之前回調(diào)(PS:一定要注意回調(diào)順序,回調(diào)順序能保證你確定LifeCycleOwner當(dāng)前所處的狀態(tài))库正。
LifeCycleOwner簡(jiǎn)介
如下圖所示
一般情況下曲楚,我們認(rèn)為Activity/Fragment/Service等這些有嚴(yán)格生命周期函數(shù)的組件為L(zhǎng)ifeCycleOwner,而ViewModal褥符,LiveData則可以做為觀察者龙誊,訂閱這些組件的生命周期變化,在生命周期變化時(shí)更新UI喷楣。
其中在源碼中有些生命周期組件都已經(jīng)實(shí)現(xiàn)了LifeCycleOwner接口趟大,并不需要我們自己去實(shí)現(xiàn),例如AppCompactActivity铣焊,下面是他的繼承關(guān)系圖:
具體在使用過(guò)程中逊朽,可查看源碼,如果源碼內(nèi)沒(méi)有實(shí)現(xiàn)LifeCycleOwner接口曲伊,就需要自己實(shí)現(xiàn)了惋耙。
自己實(shí)現(xiàn)LifeCycleOwner
上面講到了,系統(tǒng)中的部分組件已經(jīng)實(shí)現(xiàn)了LifeCycleOwner 接口,那對(duì)于系統(tǒng)沒(méi)有實(shí)現(xiàn)的绽榛,我們應(yīng)該怎么做呢湿酸?(這里以android.app.Fragment為例說(shuō)明)。
在自己實(shí)現(xiàn)LifeCycleOwner過(guò)程中灭美,需要LifecycleRegistry類打輔助:
LifeCycleRegistry的官網(wǎng)說(shuō)明如下:
An implementation of Lifecycle that can handle multiple observers.
It is used by Fragments and Support Library Activities. You can also directly use it if you have a custom LifecycleOwner.
意思是說(shuō)它是LifeCycle接口的一個(gè)實(shí)現(xiàn)推溃,其可以處理多個(gè)觀察者同時(shí)監(jiān)聽(tīng)的情況,通常情況下届腐,是被支持庫(kù)中的Activity和Fragment使用的铁坎,當(dāng)然當(dāng)你需要自己實(shí)現(xiàn)一個(gè)LifeCycleOwner時(shí),也可以直接使用它(引用英文并非為了裝13犁苏,而是希望大家能夠養(yǎng)成閱讀官方文檔的習(xí)慣硬萍。)
Ok,那么接下來(lái)讓我們一起實(shí)現(xiàn)吧围详,一般有如下三步:
- 實(shí)現(xiàn)LifeCycleOwner接口
- 初始化LifeCycleRegistry對(duì)象并返回
- 注入生命周期狀態(tài)
實(shí)現(xiàn)LifeCycleOwner接口
首先新建一個(gè)類朴乖,繼承自android.app.Fragment 并實(shí)現(xiàn)LifeCycleOwner接口
如下所示:
public class LifeCycleFragment extends Fragment implements LifecycleOwner {
@NonNull
@Override
public Lifecycle getLifecycle() {
return null;
}
}
初始化LifeCycleRegistry對(duì)象并返回
public class LifeCycleFragment extends Fragment implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
注入生命周期狀態(tài)
public class LifeCycleFragment extends Fragment implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;
public LifeCycleFragment(){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(State.CREATED);
}
@Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(State.STARTED);
}
@Override
public void onResume() {
super.onResume();
mLifecycleRegistry.markState(State.RESUMED);
}
@Override
public void onDestroy() {
super.onDestroy();
mLifecycleRegistry.markState(State.DESTROYED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
LifeCycleObserver簡(jiǎn)介
上面說(shuō)過(guò)LifeCycleObserver是監(jiān)聽(tīng)組件生命周期變化的接口,關(guān)于它的官網(wǎng)說(shuō)明如下:
Marks a class as a LifecycleObserver. It does not have any methods, instead, relies on {OnLifecycleEvent} annotated methods.
從上可以看出它是一個(gè)標(biāo)記接口助赞,只是用來(lái)標(biāo)記這個(gè)類是一個(gè)監(jiān)聽(tīng)生命周期變化的類买羞,并不具有任何函數(shù),而要真正監(jiān)聽(tīng)生命周期變化雹食,需要依賴于OnLifecycleEvent注解來(lái)實(shí)現(xiàn)畜普。
編碼實(shí)現(xiàn)LifeCycleObserver
編寫一個(gè)LifeCycleObserver大概有兩步:
1.根據(jù)Java版本實(shí)現(xiàn)LifeCycleObserver接口,添加OnLifecycleEvent注解回調(diào)
2.綁定Observer到LifeCycleOwner
根據(jù)Java版本實(shí)現(xiàn)LifeCycleObserver接口群叶,添加OnLifecycleEvent注解回調(diào)
對(duì)于LifeCycleObserver而言吃挑,不同的Java版本實(shí)現(xiàn)不同,Java 8可用如下方式實(shí)現(xiàn):
class TestObserver implements DefaultLifecycleObserver {
@Override
public void onCreate(LifecycleOwner owner) {
// your code
}
}
Java 8以下用注解方式實(shí)現(xiàn)
class MyLifeCycleObserver implements LifecycleObserver{
private Context context;
private CallBack callBack;
MyLifeCycleObserver(Context context,CallBack callBack){
this.context = context;
this.callBack = callBack;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void start(){
Log.e("LifeCycleObserverDemo","Activity started,do init things");
callBack.start();
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stop(){
Log.e("LifeCycleObserverDemo","Activity stoped,do stop things");
callBack.stop();
}
}
上述代碼中CallBack為執(zhí)行完Observer內(nèi)部事件的回調(diào)街立,內(nèi)容如下:
interface CallBack{
void start();
void stop();
}
綁定Observer到LifeCycleOwner
使用如下方式綁定LifeCycleObserver到LifeCycleOwner
myLifeCycleObserver = new MyLifeCycleObserver(this, new CallBack() {
@Override
public void start() {
Log.e("LifeCycleObserverDemo","Activity started,do init things end,update ui");
}
@Override
public void stop() {
Log.e("LifeCycleObserverDemo","Activity stoped,do stop things end,update ui");
}
});
getLifecycle().addObserver(myLifeCycleObserver);
在LifeCycleOwner Destory時(shí)記得解綁:
@Override
protected void onDestroy() {
super.onDestroy();
getLifecycle().removeObserver(myLifeCycleObserver);
}
根據(jù)上文編寫Observer綁定到Owner的log輸出如下:
[圖片上傳失敗...(image-ad64a9-1512484508738)]
最新文章會(huì)更新在我的公眾號(hào)上舶衬,歡迎大家關(guān)注
[圖片上傳失敗...(image-abeded-1512484508738)]