Android Architecture Components

作者: nothingwxq | 来源:发表于2018-04-09 19:46 被阅读66次

    一 背景

    google的Architecture Components Components(lz 简写AAC)出来好久了,但一直没时间阅读源码,趁最近空挡,阅读了AAC的源码,分享下阅读的理解。

    二 AAC是个什么东西

    其实,AAC就是google提出的一种app开发框架,里面最基础的应该就是Lifecycle-Aware了。 在这之前,我们组开发采用的mvvm + data binding 模式。 这个模式中,我们常常这样一个需求, 我们需要在fragment onDestroy 或 onPause时,反注册ViewModel中释放资源, 需要层层经过, ViewModel -- > adapter--> fragment ViewModel ——> fragment等几级回调。而Lifecycle-Aware 这时就有一个优势,就是跟生命周期绑定, 直接在相应生命周期逻辑处理好就好。当然,这里也是一种解耦方式,采用观察者模式实现。

    三 Lifecycle-Aware 的源码

    观察者模式

    a 首先找使用入口, 一般我们是这样使用Lifecycle-Aware的,
    Lifecycle lifecycle = lifecycleOwner.getLifecycle();
            lifecycle.addObserver((GenericLifecycleObserver) (source, event) -> {
               
            });
    
    b lifecycle的addObserver方法,如下:
    @Override
        public void addObserver(LifecycleObserver observer) {
            State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
            ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
            ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
    
          //已添加
            if (previous != null) {
                return;
            }
    
           //第二次重入
            boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
    
            State targetState = calculateTargetState(observer);
            mAddingObserverCounter++;
            while ((statefulObserver.mState.compareTo(targetState) < 0
                    && mObserverMap.contains(observer))) {
                pushParentState(statefulObserver.mState);
                statefulObserver.dispatchEvent(mLifecycleOwner, 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--;
        }
    

    statefulObserver.dispatchEvent(mLifecycleOwner, upEvent(statefulObserver.mState))一句就会调用 mLifecycleObserver.onStateChanged(owner, event);即我们上述入口的GenericLifecycleObserver的 void onStateChanged(LifecycleOwner source, Lifecycle.Event event)方法。

    c 特别说明下,ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
     ObserverWithState(LifecycleObserver observer, State initialState) {
                mLifecycleObserver = Lifecycling.getCallback(observer);
                mState = initialState;
            }
    
    d 包装Observer为 GenericLifecycleObserver。 这里我们注意到,使用了反射。 同事在使用时,遇到一个bug就是正式包会crash, 原因就是此版本的混淆文件没对Lifecycling类进行keep。
    static GenericLifecycleObserver getCallback(Object object) {
            if (object instanceof GenericLifecycleObserver) {
                return (GenericLifecycleObserver) object;
            }
            //noinspection TryWithIdenticalCatches
            try {
                final Class<?> klass = object.getClass();
                Constructor<? extends GenericLifecycleObserver> cachedConstructor = sCallbackCache.get(
                        klass);
                if (cachedConstructor != null) {
                    return cachedConstructor.newInstance(object);
                }
                cachedConstructor = getGeneratedAdapterConstructor(klass);
                if (cachedConstructor != null) {
                    if (!cachedConstructor.isAccessible()) {
                        cachedConstructor.setAccessible(true);
                    }
                } else {
                    cachedConstructor = sREFLECTIVE;
                }
                sCallbackCache.put(klass, cachedConstructor);
                return cachedConstructor.newInstance(object);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    
    e 另外, sync()方法如下:
    // happens only on the top of stack (never in reentrance),
        // so it doesn't have to take in account parents
        private void sync() {
            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();
                }
                Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
                if (!mNewEventOccurred && newest != null
                        && mState.compareTo(newest.getValue().mState) > 0) {
                    forwardPass();
                }
            }
            mNewEventOccurred = false;
        }
    
    f 如果处于初始化周期,onResume及以前forwardPass(),正向回调之前的生命周期。 onPause及以后, 逆向回调backwardPass()。

    这时,特别数据结构FastSafeIterableMap就比较优势,支持正逆向遍历。 如果当前状态小于以前状态, 正向回调, 否则逆向回调。这里处理与fragment生命周期状态类似。状态机,贴一下官方盗图:

    image.png

    trick 1 无ui fragment 绑定命周期

    a 目前Android sdk 26.1.0 已支持了 Lifecycle-Aware。 我们先选取Activity 来看。Lifecycle-Aware的 初始化逻辑在SupportActivity , BaseFragmentActivityApi14 extends SupportActivity,可以看出最低版本是sdk 14 , 最后继承的是我们最熟悉的子类是FragmentActivity extends BaseFragmentActivityApi16。
    image.png
    b 这里是整个AAC常见的套路, 使用无ui fragment来同步activity的生命周期。具体ReportFragment inject的代码如下。
     public static void injectIfNeededIn(Activity activity) {
            // ProcessLifecycleOwner should always correctly work and some activities may not extend
            // FragmentActivity from support lib, so we use framework fragments for activities
            android.app.FragmentManager manager = activity.getFragmentManager();
            if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
                manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
                // Hopefully, we are the first to make a transaction.
                manager.executePendingTransactions();
            }
        }
    
    c 代码比较简单在create等回调,dispatch对应的事件。
     @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            dispatchCreate(mProcessListener);
            dispatch(Lifecycle.Event.ON_CREATE);
        }
    

    给对应实现了的LifecycleRegistryOwner或LifecycleOwner activity回调

    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);
                }
            }
        }
    
     public void handleLifecycleEvent(Lifecycle.Event event) {
            mState = getStateAfter(event);
            if (mHandlingEvent || mAddingObserverCounter != 0) {
                mNewEventOccurred = true;
                // we will figure out what to do on upper level.
                return;
            }
            mHandlingEvent = true;
            sync();
            mHandlingEvent = false;
        }
    

    这时会去同步一遍状态

    sample trick 2

    另外一个小的点是,在BaseSample中使用ContentProvider onCreate中绑定activity和fragment生命周期,这点就不详述了。

    public class ProcessLifecycleOwnerInitializer extends ContentProvider {
        @Override
        public boolean onCreate() {
            LifecycleDispatcher.init(getContext());
            ProcessLifecycleOwner.init(getContext());
            return true;
        } 
      。。。
    }
    
    image.png

    继续盗图,左边部分就是目前的回调。

    相关文章

      网友评论

        本文标题:Android Architecture Components

        本文链接:https://www.haomeiwen.com/subject/jisicftx.html