本文主要是介绍Android JetPack Lifecycle源码解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
是什么?
JetPack Lifecycle是一个页面(Activity/Fragment)生命周期的组件,当你想监听生命周期,那么就可以使用这个组件。
怎么用?
第一种:
getLifecycle().addObserver(new LifecycleObserver() {@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)public void onCreate(){}@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)public void onDestory(){}})
第二种:
getLifecycle().addObserver(new LifecycleEventObserver() {@Overridepublic void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {}});
如何实现的?
相关类图:
1.FragmentActivity 实现了 LifecycleOwner借口,该接口有一个方法:getLifecycle
@NonNull@Overridepublic Lifecycle getLifecycle() {return mLifecycleRegistry;}
mLifecycleRegistry 是 在FragmentActivity 成员变量里面有一个LifecycleRegistry
final LifecycleRegistry mFragmentLifecycleRegistry = new LifecycleRegistry(this);
2.也就是当我们调用getLifeCycle 就会拿到LifecycleRegistry,该类实现了Lifecycle 借口。
然后调用addObserver 方法
@Overridepublic void addObserver(@NonNull LifecycleObserver observer) {State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);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 quicklyreturn;}boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;State targetState = calculateTargetState(observer);mAddingObserverCounter++;while ((statefulObserver.mState.compareTo(targetState) < 0&& mObserverMap.contains(observer))) {pushParentState(statefulObserver.mState);statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState));popParentState();// mState / subling may have been changed recalculatetargetState = calculateTargetState(observer);}if (!isReentrance) {// we do sync only on the top level.sync();}mAddingObserverCounter--;}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;}}
new ObserverWithState(observer, initialState); 这里最终会调用 androidx.lifecycle.Lifecycling#resolveObserverCallbackType 去解析注解,然后把 Obaserver 加入到 private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap =
new FastSafeIterableMap<>(); 里面去。
这里为什么用一个Map 去存储呢? 因为他需要解析 LifecycleObserver 这个类里面的监听方法存储起来,当生命周期发生变化的时候,进行回调。当调用remove方法移除掉LifeCycleObserver 的时候,还是需要用到原来的Observer对象,所以这里用到了Map.
4.当生命周期发生改变的时候,会调用handleLifecycleEvent分发生命周期的改变给观察者。
/*** Destroy all fragments.*/@Overrideprotected void onDestroy() {super.onDestroy();mFragments.dispatchDestroy();mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);}
总结:
使用到了观察者模式,当生命周期发生变化的时候,回调给监听者。
这篇关于Android JetPack Lifecycle源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!