作者:g小志
转载地址:https://www.jianshu.com/u/70a8f4edb323
Lifecycle生命周期感知型组件,用来执行、操作、响应另一个组件(如 Activity 和 Fragment)的生命周期状态的变化。
本文Lifecycle版本为2.2.0:
implementation ‘androidx.lifecycler:lifecycle-common:2.2.0’ //22.2.23 更新 2.5 ,2.2是用的比较多的版本
Lifecycle使用非常非常简单。默认你已经使用过Lifecycle。但如果我问你以下几个问题。你能回答出来几个?
如果我是面试官,遇到简历上写掌握Jetpack组件,我一定是会问Lifecycle这几个问题。因为它首先是Jetpack另外两个超级核心组件ViewModel,LiveData,实现能力的基石。同时它的使用频率也非常高,看似简单,容易被忽略,但却很多东西值得学习。
这十几个问题,从用法到源码,从表面到延伸。如果全懂,说明你真正掌握了Lifecycle的80%,没错~!仅仅是8成。因为下面的源码分析,还会有更多延伸的问题。
作为生命周期感知组件、它的作用就是监听宿主Activity/Fragment,然后派发给观察者。这句看似简单的概括,却倒出3个重要的角色:
宿主,观察者,用来派发的调度器
使用Lifecycle的方法很简单。
先创建Observer,可以直接继承父类LifecycleObserver。
public class LocationObserver implements LifecycleObserver {
private static final String TAG = "LocationObserver";
//1. 自定义的LifecycleObserver观察者,用注解声明每个方法观察的宿主的状态
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void onCreate(@NotNull LifecycleOwner owner) {
Timber.e("onCreate_ON_CREATE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void onStart(@NotNull LifecycleOwner owner) {
Timber.e("onStart_ON_START");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onResume(@NotNull LifecycleOwner owner) {
Timber.e("onResume_ON_RESUME");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void onPause(@NotNull LifecycleOwner owner) {
Timber.e("onPause_ON_PAUSE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void onStop(@NotNull LifecycleOwner owner) {
Timber.e("onStop_ON_STOP");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(@NotNull LifecycleOwner owner) {
Timber.e("onDestroy_ON_DESTROY");
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onAny(LifecycleOwner owner) {
Timber.e("onAny_ON_ANY-->%s", owner.getLifecycle().getCurrentState());
}
}
自己定义生命周期方法,并在每个方法上面标记对应生命周期的注解。同时还能获得LifecycleOwner也就是宿主Activity。
继承LifecycleEventObserver,复写onStateChanged方法。
public class EventObserver implements LifecycleEventObserver {
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
switch (event) {
case ON_CREATE:
Timber.d("ON_CREATE");
break;
case ON_START:
Timber.d("ON_START");
break;
case ON_RESUME:
Timber.d("ON_RESUME");
break;
case ON_PAUSE:
Timber.d("ON_PAUSE");
break;
case ON_STOP:
Timber.d("ON_STOP");
break;
case ON_DESTROY:
Timber.d("ON_DESTROY");
break;
default:
break;
}
}
}
继承FullLifecycleObserver,直接复写对应的生命周期回调。
public class FullLocationObserver implements DefaultLifecycleObserver {
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
Timber.e("onCreate");
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
Timber.e("onStart");
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
Timber.e("onResume");
}
@Override
public void onPause(@NonNull LifecycleOwner owner) {
Timber.e("onPause");
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
Timber.e("onStop");
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
Timber.e("onDestroy");
}
}
此时你会发现,你无法直接继承FullLifecycleObserver这时你要添加一个依赖:
implementation ‘androidx.lifecycler:lifecycle-common:2.2.0’
替换上面引入的依赖(默认会集成上面的)
implementation “androidx.lifecycle:lifecycle-common-java8:2.2.0”
继承DefaultLifecycleObserver,它是FullLifecycleObserver的子类,因为在Java8以后,支持interface接口类型,可以有自己的默认实现。然后在Activity#OnCreate()中调用如下方法:
lifecycle.addObserver(LocationObserver())
接着,你就可以使用Lifecycle的能力了。
如果是MVP,你可以让你的Presenter去实现Observer,在处理逻辑时获得感知的能力。但如果我们把注册的代码lifecycle.addObserver(LocationObserver())放入onResume()方法中,会发生什么?你会发现Observer除了可以收到onResume事件,竟然还可以收到onCreate,onStart。也就说宿主的状态,会同步给观察者。这是怎么做到的?
感知监听
getLifecycle点进去,会进入到ComponentActivity核心代码。
public class ComponentActivity extends Activity implements LifecycleOwner{
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
protected void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);
//往Activity上添加一个fragment,用以报告生命周期的变化
//目的是为了兼顾不是继承自AppCompactActivity的场景.
ReportFragment.injectIfNeededIn(this);
}
public class ReportFragment extends Fragment{
public static void injectIfNeededIn(Activity activity) {
android.app.FragmentManager manager = activity.getFragmentManager();
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
manager.executePendingTransactions();
}
}
@Override
public void onStart() {
super.onStart();
dispatch(Lifecycle.Event.ON_START);
}
@Override
public void onResume() {
super.onResume();
dispatch(Lifecycle.Event.ON_RESUME);
}
@Override
public void onPause() {
super.onPause();
dispatch(Lifecycle.Event.ON_PAUSE);
}
@Override
public void onDestroy() {
super.onDestroy();
dispatch(Lifecycle.Event.ON_DESTROY);
}
private void dispatch(Lifecycle.Event event) {
//调用宿主的Lifecycle
Lifecycle lifecycle = activity.getLifecycle();
if (lifecycle instanceof LifecycleRegistry) {
((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
}
}
ReportFragment.injectIfNeededIn(this)的作用是在Activity之上,创建一个不可见的Fragment的。当Fragment的生命周期发生变化,会通过dispatch(),接着调用((LifecycleRegistry) lifecycle).handleLifecycleEvent(event)来分发事件。这样做的目的是为了当页面不是继承ComponentActivity,而是直接继承Activity,那么它就没有了感知的能力。此时需要自己实现LifecycleOwner,复写getLifecycle(),然后将自己传入进new LifecycleRegistry(this),就可以成为宿主,让其他Observer来监听。ReportFragment.injectIfNeededIn(this)另一个使用的地方LifecycleDispatcher。
class LifecycleDispatcher {
private static AtomicBoolean sInitialized = new AtomicBoolean(false);
static void init(Context context) {
if (sInitialized.getAndSet(true)) {
return;
}
((Application) context.getApplicationContext())
.registerActivityLifecycleCallbacks(new DispatcherActivityCallback());
}
@SuppressWarnings("WeakerAccess")
@VisibleForTesting
static class DispatcherActivityCallback extends EmptyActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
ReportFragment.injectIfNeededIn(activity);
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
}
private LifecycleDispatcher() {
直接为Activity注入ReportFragment,使得每个页面都能成为宿主,让观察者感知。当然,你里面要自己实现LifecycleOwner,复写getLifecycle(),然后将自己传入进new LifecycleRegistry(this)。这部分与Lifecycle关系不大,只是作为知识扩展
总结
为什么以Activity为例子,而不是像其他文章,以Fragement为例子,是因为你去看眼源码就会发现ReportFragment类中的dispatch()过程和Fragement如出一辙:
ublic class Fragment implements xxx, LifecycleOwner {
//...
void performCreate(Bundle savedInstanceState) {
onCreate(savedInstanceState); //1.先执行生命周期方法
//...省略代码
//2.生命周期事件分发
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
}
void performStart() {
onStart();
//...
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
void performResume() {
onResume();
//...
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
}
void performPause() {
//3.注意,调用顺序变了
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
//...
onPause();
}
void performStop() {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
//...
onStop();
}
void performDestroy() {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
//...
onDestroy();
}
}
参照《Android 架构组件(一)——Lifecycle》一文:
https://blog.csdn.net/zhuzp_blog/article/details/78871374?spm=1001.2014.3001.5501
同时,你会发现Fragment中performCreate()、performStart()、performResume()会先调用自身的onXXX()方法,然后再调用LifecycleRegistry的handleLifecycleEvent()方法;而在performPause()、performStop()、performDestroy()中会先LifecycleRegistry的handleLifecycleEvent()方法,然后调用自身的onXXX()方法。
同步
上面是监听过程,getLifecycle().addObserver(LocationObserver())注册代码的前半部分。现在我们获得感知宿主的能力,并将事件event转发给LifecycleRegistry,他是整个流程的核心。现在来看后半部分,如何注册Observer后可以在任何时机(onDestory除外),感知完整生命周期。
不过在此之前,我们要先搞懂什么是状态State和事件Event。
public abstract class Lifecycle {
@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer);
@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer);
@MainThread
@NonNull
public abstract State getCurrentState();
public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
}
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
/**
* after {@link android.app.Activity#onStart() onStart} call;
* right before {@link android.app..Activity#onPause( onPause} call.
*
*/
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
}
在Lifecycle类中除了add,remove外,还有两个枚举类,Event事件对应的就是Activty的onCreate,onStart()事件,而状态却只有5种。以STARTED状态为例,这个状态发生在Activity#onStart()之后,Activity#onPause()之前。什么意思呢?请看下面这张图:
这张图我们一定见过,但这次我们横着对半切一刀,上面表示生命周期的前进,下面表示生命周期的后退,一定要记住,这对我们后的理解源码非常重要,接着我们在解释下:
Activity刚刚创建的时候它一定是INITIALIZED状态,执行onCreate()方法后,进入到CREATED状态,执行onStart()方法后,进入到STARTED状态,执行onResume()方法后,进入到RESUMED状态,这个过程就表示生命周期的前进。
**当我们跳转下一个Activity后,执行onPase()方法后,又重新回到STARTED状态,执行onStop()方法后,进入到CREATED状态,最后执行onDestory()方法后,进入到DESTROYED状态。这个过程表示生命周期的后退。**参照《Android 架构组件(一)——Lifecycle》一文:
https://blog.csdn.net/zhuzp_blog/article/details/78871374?spm=1001.2014.3001.5501
文中的关系图:
这个图片仅作参考,ObserverWithState类中的关系有所变换,mLifeCycleObserver->LifeCycleEventObserver。LifecycleRegistryshi生命周期注册,记录,派发事件的地方,理解状态和事件的关系,对我们搞清楚LifecycleRegistry非常有帮助,下面以在onResume()中调用lifecycle.addObserver(LocationObserver())为例:
public LifecycleRegistry(@NonNull LifecycleOwner provider) {
mLifecycleOwner = new WeakReference<>(provider);
mState = INITIALIZED;
}
public void addObserver(@NonNull LifecycleObserver observer) {
//1
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
//2
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
//3
ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
if (previous != null) {
return;
}
LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
if (lifecycleOwner == null) {
// it is null we should be destroyed. Fallback quickly
return;
}
boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
//4
State targetState = calculateTargetState(observer);
mAddingObserverCounter++;
while ((statefulObserver.mState.compareTo(targetState) < 0
&& mObserverMap.contains(observer))) {
pushParentState(statefulObserver.mState);
//5
statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));
popParentState();
// mState / subling may have been changed recalculate
targetState = calculateTargetState(observer);
}
if (!isReentrance) {
// we do sync only on the top level.
sync();
}
mAddingObserverCounter--;
}
//并不是继承自`Observer`
static class ObserverWithState {
State mState;
LifecycleEventObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {
mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
mState = initialState;
}
void dispatchEvent(LifecycleOwner owner, Event event) {
State newState = getStateAfter(event);
mState = min(mState, newState);
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}
private State calculateTargetState(LifecycleObserver observer) {
Entry previous = mObserverMap.ceil(observer);
State siblingState = previous != null ? previous.getValue().mState : null;
State parentState = !mParentStates.isEmpty() ? mParentStates.get(mParentStates.size() - 1)
: null;
return min(min(mState, siblingState), parentState);
}
private static Event upEvent(State state) {
switch (state) {
case INITIALIZED:
case DESTROYED:
return ON_CREATE;
case CREATED:
return ON_START;
case STARTED:
return ON_RESUME;
//发生生命周期的前进,RESUMED处于前进阶段的末尾,无法前进了。因为之后要发生生命周期后退
//所以抛出异常
case RESUMED:
throw new IllegalArgumentException();
}
throw new IllegalArgumentException("Unexpected state value " + state);
}
//根据事件,判断所处的状态
static State getStateAfter(Event event) {
switch (event) {
case ON_CREATE:
case ON_STOP:
return CREATED;
case ON_START:
case ON_PAUSE:
return STARTED;
case ON_RESUME:
return RESUMED;
case ON_DESTROY:
return DESTROYED;
case ON_ANY:
break;
}
throw new IllegalArgumentException("Unexpected event value " + event);
}
经过循环之后。新注册的Observer和宿主同步到相同的生命周期。
派发
当宿主生命周期发生变化,会调用mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)来向观察者派发,直接看分析然后再结合源码。
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
State next = getStateAfter(event);
moveToState(next);//1
}
private void moveToState(State next) {
mState = next;
sync();//2
}
private void sync() {
while (!isSynced()) {//3
//当宿主状态小于 最早添加进来的观察者,为什么是最早,首先集合中的观察者是按顺序添加的,State应该是一致的,小于最早观察者,一定小于后面最后观察者
if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
backwardPass(lifecycleOwner);//4
}
Entry newest = mObserverMap.newest();
if (!mNewEventOccurred && newest != null
//当宿主状态大于最新的道理相同
&& mState.compareTo(newest.getValue().mState) > 0) {
forwardPass(lifecycleOwner);//5
}
}
}
private boolean isSynced() {
if (mObserverMap.size() == 0) {
return true;
}
State eldestObserverState = mObserverMap.eldest().getValue().mState;
State newestObserverState = mObserverMap.newest().getValue().mState;
return eldestObserverState == newestObserverState && mState == newestObserverState;
}
private void backwardPass(LifecycleOwner lifecycleOwner) {
while ((observer.mState.compareTo(mState) > 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {
Event event = downEvent(observer.mState);
observer.dispatchEvent(lifecycleOwner, event);
}
}
private void forwardPass(LifecycleOwner lifecycleOwner) {
while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {
observer.dispatchEvent(lifecycleOwner, upEvent(observer.mState));
}
}
private static Event downEvent(State state) {
switch (state) {
//最初的状态 无法再后退
case INITIALIZED:
throw new IllegalArgumentException();
case CREATED:
return ON_DESTROY;
case STARTED:
return ON_STOP;
case RESUMED:
return ON_PAUSE;
//最末的状态 无法再回退
case DESTROYED:
throw new IllegalArgumentException();
}
throw new IllegalArgumentException("Unexpected state value " + state);
}
统一Observer
思考一个问题
ObserverWithState#dispatch是给观察者分发事件的位置,但他调用的是onStateChange(),但我们回调方式的实现,是完全不同的,他是如何做到统一的呢?
static class ObserverWithState {
State mState;
LifecycleEventObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {
mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
mState = initialState;
}
void dispatchEvent(LifecycleOwner owner, Event event) {
State newState = getStateAfter(event);
mState = min(mState, newState);
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}
mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer)答案就是适配器模式,看就在这里。
@NonNull
static LifecycleEventObserver lifecycleEventObserver(Object object) {//Object 就是传入的observer
boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver;
boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver;
if (isLifecycleEventObserver && isFullLifecycleObserver) {
return new FullLifecycleObserverAdapter((FullLifecycleObserver) object,
(LifecycleEventObserver) object);
}
if (isFullLifecycleObserver) {
return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null);
}
if (isLifecycleEventObserver) {
return (LifecycleEventObserver) object;
}
final Class> klass = object.getClass();
//关键代码
int type = getObserverConstructorType(klass);
if (type == GENERATED_CALLBACK) {
List> constructors =
sClassToAdapters.get(klass);
if (constructors.size() == 1) {
GeneratedAdapter generatedAdapter = createGeneratedAdapter(
constructors.get(0), object);
return new SingleGeneratedAdapterObserver(generatedAdapter);
}
GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()];
for (int i = 0; i < constructors.size(); i++) {
adapters[i] = createGeneratedAdapter(constructors.get(i), object);
}
return new CompositeGeneratedAdaptersObserver(adapters);
}
return new ReflectiveGenericLifecycleObserver(object);
}
而FullLifecycleObserverAdapter ,所有适配器Adapter都是继承自LifecycleEventObserver。这样就可以收拢统一后调用。而直接继承LifecycleObserver又是怎么判断的呢?
int type = getObserverConstructorType(klass);
最终调用
/**
* Create a name for an adapter class.
*/
public static String getAdapterName(String className) {
return className.replace(".", "_") + "_LifecycleAdapter";
}
当引入androidx.lifecycle:lifecycle-compiler:2.2.0会利用APT,运行时注解处理器生成工具类并拼接类名MyObserver_LifecycleAdapter,然后调取注解标记的观察者,否者会用ReflectiveGenericLifecycleObserver,反射执行。嵌套事件这部分不好理解,目前个人的理解就是,先弄清楚什么时候回出现嵌套事件,两种情况:
嵌套事件,说白了就是同步冲突。因为所有观察者和宿主的状态是完全相同的,一旦生命周期派发,或是新的观察者添加进来时,如果生命周期或观察者集合元素发生变化,就可能会导致不同步。这错乱不是来自多线程。因为细心的你会发现。LifeCycleRegistry的大部分方法都标注@MainThread注解。也就说,冲突并非是多线程,而是上面两种情况。
那么LifeCycleRegistry又是如何如何处理的呢?答案是利用多个标记位表示状态,同时将新添加的观察者,或是正在变化的观察者压入栈。正确同步后后再出栈,也就是mParentStates这个mParentStates类型的集合。所以一旦出现观察者状态不一致导致无法正常出栈。这个栈mParentStates内的元素就不是空。就需要来重新处理或者判断。核心思想大概是这样样子。就是多个标记位和栈管理,来确保状态的同步和一致。
以上是个人理解。对嵌套事件感兴趣,想深入理解,可以看这篇:
https://blog.csdn.net/quiet_olivier/article/details/103384146?spm=1001.2014.3001.5502
LifeCycle组件简单,深入却有很复杂,理解后又会发现很有意思。但大部分文章却无法做到既深入,又能体现却他人的不同。更无法完全解答开头的十几个问题。不过现在你应该有了自己的答案。