对于LifeCycle的简单使用可以看上一篇文章:LiveData+ViewModel+Repository搭建MVVM
这篇文章主要是为了阅读源码,尽量深入阅读,能力还是一般,见谅:
LifeCycle的作用
解决onCreate等生命周期方法,由于各种原因,后期越来越臃肿的问题。
关键类阐述
LifecycleRegistryOwner/LifecycleOwner
在Activity等组件生命周期发生变化的时候,发出相应的Event给LifecycleRegistry。
LifecycleRegistry
控制state的转换、接受分发Event事件。
LifecycleObserver
通过注解绑定Event和自定义的函数,实现对生命周期的监听并处理。
Event
@SuppressWarnings("WeakerAccess")
public enum Event {
/**
* Constant for onCreate event of the {@link LifecycleOwner}.
*/
ON_CREATE,
/**
* Constant for onStart event of the {@link LifecycleOwner}.
*/
ON_START,
/**
* Constant for onResume event of the {@link LifecycleOwner}.
*/
ON_RESUME,
/**
* Constant for onPause event of the {@link LifecycleOwner}.
*/
ON_PAUSE,
/**
* Constant for onStop event of the {@link LifecycleOwner}.
*/
ON_STOP,
/**
* Constant for onDestroy event of the {@link LifecycleOwner}.
*/
ON_DESTROY,
/**
* An {@link Event Event} constant that can be used to match all events.
*/
ON_ANY
}
- 阅读剩余部分 -