Android Lifecycle使用

观书散遗帙,探古穷至妙。这篇文章主要讲述Android Lifecycle使用相关的知识,希望能为你提供帮助。
引言
Lifecycle 是官方提供的架构组件之一,目前已经是稳定版本,Lifecycle 组件包括LifecycleOwner、LifecycleObserver。Lifecycle 组件是执行操作以响应另一个组件(Activity或者Fragment)的生命周期状态的更改。 Lifecycle 生成更易于组织、更轻量级,更易于维护的代码。
不使用Lifecycle
在使用MVP模式中,如果需要Presenter感知Activity或者Fragment的生命周期,传统做法是Presenter中定义多个和Activity或者Fragment相应的生命周期方法,然后在Activity或者Fragment中调用Presenter中定义的方法,例如:

class UserPresenter(view: IUserView) { private val mView = view private val mModel: UserModel = UserModel()fun onStart(){ // 初始化一些信息 }fun onDestroy(){ // 释放一起请求 } } class MainActivity : AppCompatActivity(), IUserView {private val userPresenter = UserPresenter(this)override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) if (savedInstanceState == null) { supportFragmentManager.beginTransaction() .replace(R.id.container, MainFragment.newInstance()) .commitNow() }}override fun onStart() { super.onStart() userPresenter.onStart() }override fun onDestroy() { super.onDestroy() userPresenter.onDestroy() }override fun onLoading() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. }override fun onSuccess() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. }override fun onComplete() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }

实际项目中,需求可能比较复杂,这样会导致太多类似的调用,而使onStart()和onDestroy() 方法变的非常臃肿。
使用Lifecycle
定义IPresenter 接口类,继承LifecycleObserver ,其它具体的Presenter继承该类,方便使用。
interface IPresenter : LifecycleObserver {@OnLifecycleEvent(Lifecycle.Event.ON_START) fun onStart(owner: LifecycleOwner)@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun onDestroy(owner: LifecycleOwner) }

定义一个具体的UserPresenter,继承IPresenter
class UserPresenter(view: IUserView) : IPresenter { private val mView = view private val mModel: UserModel = UserModel()companion object { const val TAG: String = "UserPresenter" }override fun onStart(owner: LifecycleOwner) { Log.d(TAG,"onStart and Presenter") }override fun onDestroy(owner: LifecycleOwner) { Log.d(TAG,"onDestroy and Presenter") } }

在Activity或者Fragment使用
class MainActivity : AppCompatActivity(), IUserView {companion object { val TAG: String = "MainActivity" }private val userPresenter = UserPresenter(this)override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) if (savedInstanceState == null) { supportFragmentManager.beginTransaction() .replace(R.id.container, MainFragment.newInstance()) .commitNow() } lifecycle.addObserver(userPresenter)// 订阅事件 }override fun onStart() { super.onStart() Log.d(TAG,"onStart and UI") }override fun onDestroy() { super.onDestroy() Log.d(TAG,"onDestroy and UI") }override fun onLoading() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. }override fun onSuccess() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. }override fun onComplete() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }

【Android Lifecycle使用】运行查看下日志
2018-12-15 12:39:50.715 2679-2679/com.fomin.arch D/MainActivity: onStart and UI 2018-12-15 12:39:50.715 2679-2679/com.fomin.arch D/UserPresenter: onStart and Presenter 2018-12-15 12:39:53.876 2679-2679/com.fomin.arch D/UserPresenter: onDestroy and Presenter 2018-12-15 12:39:53.877 2679-2679/com.fomin.arch D/MainActivity: onDestroy and UI

这样Presenter 感知Activity或Fragment的生命周期已经实现,在Activity或Fragment生命周期变化会及时通知到Presenter。无需再Activity或Fragment实现相关Presenter的生命周期感知事件,减少维护成本。
Lifecycle原理
首先了解下Lifecycle组件主要有下面一些关键类
  • LifecycleObserver :实现该接口的类,通过注解的方式,可以通过被LifecycleOwner类的addObserver方法注册,被注册后,LifecycleObserver便可以观察到LifecycleOwner的生命周期事件
  • LifecycleOwner:实现该接口的类持有生命周期(Lifecycle对象),该接口的生命周期(Lifecycle对象)的改变会被其注册的观察者LifecycleObserver观察到并触发其对应的事件
  • Event:从框架和Lifecycle类派发的生命周期事件
  • State  :由Lifecycle对象跟踪的组件的当前状态
  • LifecycleRegistry:负责控制state的转换、接受分发event事件
已Activity为例,会发现在SupportActivity里面继承了LifecycleOwner,持有LifecycleRegistry类,并且在getLifecycle()返回LifecycleRegistry类,方便继承类使用该类。
@RestrictTo(LIBRARY_GROUP) public class SupportActivity extends Activity implements LifecycleOwner {private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); @Override @SuppressWarnings("RestrictedApi") protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ReportFragment.injectIfNeededIn(this); }@CallSuper @Override protected void onSaveInstanceState(Bundle outState) { mLifecycleRegistry.markState(Lifecycle.State.CREATED); super.onSaveInstanceState(outState); }@Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } }

而在业务Activity中需要lifecycle.addObserver(LifecycleObserver)订阅事件,这样就可以生效订阅/通知事件。Event分发是通过哪里进行分发的呢?可以看下ReportFragment.injectIfNeededIn(this)这句代码,ReportFragment对各个状态使用dispatch进行事件分发,然后调用LifecycleRegistry.handleLifecycleEvent,其接受到分发的Event从而改变State。
ReportFragment代码片段 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); dispatchCreate(mProcessListener); (Lifecycle.Event.ON_CREATE); }@Override public void onStart() { super.onStart(); dispatchStart(mProcessListener); dispatch(Lifecycle.Event.ON_START); }@Override public void onResume() { super.onResume(); dispatchResume(mProcessListener); dispatch(Lifecycle.Event.ON_RESUME); }@Override public void onPause() { super.onPause(); dispatch(Lifecycle.Event.ON_PAUSE); }@Override public void onStop() { super.onStop(); dispatch(Lifecycle.Event.ON_STOP); }@Override public void onDestroy() { super.onDestroy(); dispatch(Lifecycle.Event.ON_DESTROY); // just want to be sure that we won\'t leak reference to an activity mProcessListener = null; }private void dispatch(Lifecycle.Event event) { Activity activity = getActivity(); if (activity instanceof LifecycleRegistryOwner) { ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event); return; }if (activity instanceof LifecycleOwner) { Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle(); if (lifecycle instanceof LifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); } } }LifecycleRegistry代码片段 public void handleLifecycleEvent(@NonNull Lifecycle.Event event) { State next = getStateAfter(event); moveToState(next); }private void moveToState(State next) { if (mState == next) { return; } mState = next; if (mHandlingEvent || mAddingObserverCounter != 0) { mNewEventOccurred = true; // we will figure out what to do on upper level. return; } mHandlingEvent = true; sync(); mHandlingEvent = false; } // happens only on the top of stack (never in reentrance), // so it doesn\'t have to take in account parents private void sync() { LifecycleOwner lifecycleOwner = mLifecycleOwner.get(); if (lifecycleOwner == null) { Log.w(LOG_TAG, "LifecycleOwner is garbage collected, you shouldn\'t try dispatch " + "new events from it."); return; } while (!isSynced()) { mNewEventOccurred = false; // no need to check eldest for nullability, because isSynced does it for us. if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) { backwardPass(lifecycleOwner); } Entry< LifecycleObserver, ObserverWithState> newest = mObserverMap.newest(); if (!mNewEventOccurred & & newest != null & & mState.compareTo(newest.getValue().mState) > 0) { forwardPass(lifecycleOwner); } } mNewEventOccurred = false; }

可以看下官方的对整个生命周期流动的解释
Android Lifecycle使用

文章图片

在该Lifecycle组件中,与传统的生命周期不同,只定义了五种状态,分别是:
  • INITIALIZED 最初始的状态
  • DESTROYED
  • CREATED
  • STARTED
  • RESUMED
上图中向右的箭头很好理解,每过来一个event会发生生命周期状态的变更,向左的箭头可以看成状态的回滚, 如果在RESUMED状态发生了ON_PAUSE事件,则状态回滚到STARTED状态;STARTED状态发生了ON_STOP事件,则回滚到CREATED状态。
总结
需要注意了是,在继承LifecycleObserver 时,官方建议使用DefaultLifecycleObserver ,因为随着java8成为主流,将不会再使用注解方式,DefaultLifecycleObserver是需要另外声明的java8,并且最低API24才能build success。而GenericLifecycleObserver,FullLifecycleObserver,DefaultLifecycleObserver 这三个接口都是直接或者间接继承的LifecycleObserver。
// java8的lifecycle引用 implementation "android.arch.lifecycle:common-java8:1.1.1" // 注解方式lifecycle引用 implementation \'android.arch.lifecycle:extensions:1.1.1\'

Lifecycle使用,保持 UI 控制器(Activity 和 Fragment)尽可能的精简,更易复用,同时和LiveData、ViewModel等使用更具有最佳实战。

    推荐阅读