主要记录一下自己看源码的过程
既然我们要感知Activity的生命周期,首先我们需要一个观察者:
interface IObserer{
//可以把你需要的生命周期单独拿出来
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate(owner:LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onLifecycleChange(owner:LifecycleOwner,event:Lifecycle.Event)
}
class MyObserver:IObserver{
fun onCreate(owner:LifecycleOwner){
//doSomeThing
}
fun onLifecycleChange(owner:LifecycleOwner,event:Lifecycle.Event){
//doSomething
}
}
观察者有了以后,就要想办法把他和生命周期关联起来
val myObserver = MyObserver()
lifecycle.addObserver(myObserver)
至此,我们已经可以在MyObserver中感知到Activity的生命周期变化
那么为什么这样就可以感知到生命周期变化呢
点开addObserver源码,发现该方法是属于Lifecycle抽象类的,那么就去找他的实现类
发现 SupportActivity中有个
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
那么就看LifecycleRegistry 的addObserver
@Override
public void addObserver(LifecycleObserver observer) {
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
// ①
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
//把myObserver和statefulObserver添加到Map里去,这里我理解为ObserverWithState是用来通知myObserver生命周期变化的
ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
......
}
SupportActivity在onCreate的时候会添加一个ReportFragment汇报生命周期变化(类如其名)
class ReportFragment
//在onActivityCreated、onStart等生命周期方法中,均会调用dispatch
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类中了
((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
}
}
}
handleLifecycleEvent最后会到这里
private void forwardPass() {
Iterator> ascendingIterator =mObserverMap.iteratorWithAdditions();
while (ascendingIterator.hasNext() && !mNewEventOccurred) {
Entry entry = ascendingIterator.next();
ObserverWithState observer = entry.getValue();
while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
&& mObserverMap.contains(entry.getKey()))) {
pushParentState(observer.mState);
//这里进行事件分发,所以事件还是靠的ObserverWithState ,而不是myObserver(之前想岔了,总想着是myObserver来分发事件,)
observer.dispatchEvent(mLifecycleOwner, upEvent(observer.mState));
popParentState();
}
}
}
接下来进入ObserverWithState.dispatchEvent方法看下
void dispatchEvent(LifecycleOwner owner, Event event) {
State newState = getStateAfter(event);
mState = min(mState, newState);
//进行事件分发 见 ① 具体实现就是靠注解+反射来实现方法调用
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
①
static class ObserverWithState {
State mState;
GenericLifecycleObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {
// ② 这里会把myObserver包装成ReflectiveGenericLifecycleObserver,最终分发事件是靠这个类
mLifecycleObserver = Lifecycling.getCallback(observer);
mState = initialState;
}
}
②
ReflectiveGenericLifecycleObserver(Object wrapped) {
//这个mWrapped 就是myObserver
mWrapped = wrapped;
mInfo = getInfo(mWrapped.getClass());
}
流程总结:(已感知生命周期变化为目的)
1、定义好我们自己的Observer(实现LifecycleObserver)
2、在Activity中通过lifecycle.addObserver(observer:LifecycleObserver)来关联生命周期
3、生命周期方法被调用时,ReportFragment向MyObserver汇报
lifecycle.addObserver里做的一些事情:
1、LifecycleRegistry把MyObserver和ObserverWithState对象添加到mObserverMap里以备后用
2、当生命周期发生变化是,ReportFragment通过SupportActivity的LifecycleRegistry 调用handleLifecycleEvent,最终从mObserverMap中取出ObserverWithState来分发事件
3、步骤1 中在创建ObserverWithState实例时,会在MyObserver外面包一层ReflectiveGenericLifecycleObserver,最终由ReflectiveGenericLifecycleObserver通过注解反射来调用MyObserver中由OnLifecycleEvent注解的方法