1.直接增加如下依靠就能夠滿意日常的工作,假如缺少哪個(gè)庫(kù),再去單獨(dú)增加就好了
implementation”android.arch.lifecycle:extensions:1.1.1″
增加這一句代碼就依靠了如下的庫(kù)。
eXj6eJ.png
2.先不談Activity和Fragment中怎么運(yùn)用,先舉一個(gè)Lifecycle的簡(jiǎn)略比如。
1ackagecom.example.lifecycledemo1;23importandroid.arch.lifecycle.Lifecycle;4importandroid.arch.lifecycle.LifecycleObserver;5importandroid.arch.lifecycle.OnLifecycleEvent;6importandroid.support.v7.app.AppCompatActivity;7importandroid.os.Bundle;8importandroid.util.Log;910publicclassMainActivityextendsAppCompatActivity{1112privatestaticfinalStringTAG=”MainActivity”;1314@Override15protectedvoidonCreate(BundlesavedInstanceState){16super.onCreate(savedInstanceState);17setContentView(R.layout.activity_main);18getLifecycle().addObserver(newMyObserver());//119}2021publicclassMyObserverimplementsLifecycleObserver{2223@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)24voidonResume(){25Log.d(TAG,”LifecyclecallonResume”);26}27@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)28voidonPause(){29Log.d(TAG,”LifecyclecallonPause”);30}31}3233@Override34protectedvoidonResume(){35super.onResume();36Log.d(TAG,”onResume”);37}3839@Override40protectedvoidonPause(){41super.onPause();42Log.d(TAG,”onPause”);4344}45}
先完成MyObserver,對(duì)ON_CREATE和ON_RESUME事件進(jìn)行監(jiān)聽(tīng)。由于在AndroidSupportLibrary26.1.0及其之后的版本,Activity和Fragment已經(jīng)默許完成了LifecycleOwner接口,所以在注釋1處能夠直接運(yùn)用getLifecycle辦法獲取Lifecycle對(duì)象,這樣MyObserver就能夠調(diào)查MainActivity的生命周期變化了,LifecycleOwner能夠理解為被調(diào)查者,MainActivity默許完成了LifecycleOwner接口,也就是說(shuō)MainActivity是被調(diào)查者。
運(yùn)行程序,打印的log如下所示。
D/MainActivity:onResume
D/MainActivity:LifecyclecallonResume
D/MainActivity:LifecyclecallonPause
D/MainActivity:onPause
3.MVP的比如
1publicclassMyPresenterimplementsIPresenter{2privatestaticfinalStringTAG=”test”;34@Override5publicvoidonResume(){6Log.d(TAG,”LifecyclecallonResume”);7}89@Override10publicvoidonPause(){11Log.d(TAG,”LifecyclecallonPause”);12}13}1415interfaceIPresenterextendsLifecycleObserver{1617@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)18voidonResume();1920@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)21voidonPause();22}
IPresenter接口承繼自LifecycleObserver接口,MyPresenter又完成了IPresenter接口,這樣MyPresenter成為了一個(gè)調(diào)查者。
接在在MainActivity中加入MyPresenter:
1publicclassMainActivityextendsAppCompatActivity{23privatestaticfinalStringTAG=”test”;4privateIPresentermPresenter;5@Override6protectedvoidonCreate(BundlesavedInstanceState){7super.onCreate(savedInstanceState);8setContentView(R.layout.activity_main);9mPresenter=newMyPresenter();10getLifecycle().addObserver(mPresenter);11}1213@Override14protectedvoidonResume(){15super.onResume();16Log.d(TAG,”onResume”);17}1819@Override20protectedvoidonPause(){21super.onPause();22Log.d(TAG,”onPause”);2324}25}
MainActivity成為了被調(diào)查者,當(dāng)它的生命周期發(fā)生變化時(shí),MyPresenter就能夠調(diào)查到,這樣就不需要在MainActivity的多個(gè)生命周期辦法中調(diào)用MyPresenter的辦法了。
打印的日志如下:
D/test:onResume
D/test:LifecyclecallonResume
D/test:LifecyclecallonPause
D/test:onPause
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹(shù)莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長(zhǎng)沙市天心區(qū)芙蓉中路三段398號(hào)新時(shí)空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運(yùn)營(yíng)網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號(hào)