模块化解耦框架RxFluxArchitecture3-订阅管理绑定生命周期

0.216字数 665阅读 63

相关系列文章

模块化解耦框架RxFluxArchitecture1-框架简介

模块化解耦框架RxFluxArchitecture2-基本功能实现

模块化解耦框架RxFluxArchitecture3-订阅管理绑定生命周期

模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入

模块化解耦框架RxFluxArchitecture5-Application多模块共存

引言

模块化解耦框架 RxFluxArchitecture 中订阅管理自动绑定生命周期

  • Store 实现androidx.lifecycle.LifecycleObserver,自动关联 View 生命周期。
  • View、Store 跟随 View 生命周期自动注册订阅、取消订阅。

生命周期图

生命周期与订阅管理.jpg

1、RxFlux接受 View 生命周期回调

1.1、RxFlux接受 Activity 生命周期回调

RxApp继承自Application,在onCreate()方法时调用registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks)方法,使RxFlux可以接受 Activity 生命周期回调。

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(mRxFlux);
    }

1.2、RxFlux接受 Fragment 生命周期回调

RxFlux在 Activity 创建时,方法onActivityCreated(Activity,Bundle)会被回调。调用registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks,boolean)方法,使RxFlux可以接受当前 Activity 管理的 Fragment 生命周期回调。

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        mActivityCounter++;
        mActivityStack.add(activity);
        if (activity instanceof FragmentActivity) {
            ((FragmentActivity) activity).getLifecycle()
                    .addObserver(new RxActivityLifecycleObserver(activity));
            ((FragmentActivity) activity).getSupportFragmentManager()
                    .registerFragmentLifecycleCallbacks(this, true);
        }
    }

2、Store 关联 View 生命周期

2.1、RxActivityLifecycleObserver关联 Activity 生命周期

RxFlux在 Activity 创建时,方法onActivityCreated(Activity,Bundle)会被回调。调用addObserver(LifecycleObserver)方法,使 Activity 生命周期订阅类RxActivityLifecycleObserver关联当前 Activity 生命周期。

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        mActivityCounter++;
        mActivityStack.add(activity);
        if (activity instanceof FragmentActivity) {
            ((FragmentActivity) activity).getLifecycle()
                    .addObserver(new RxActivityLifecycleObserver(activity));
            ((FragmentActivity) activity).getSupportFragmentManager()
                    .registerFragmentLifecycleCallbacks(this, true);
        }
    }

2.2、RxFragmentLifecycleObserver关联 Fragment 生命周期

RxFlux在 Fragment 关联当前 Activity 时,方法onFragmentAttached(FragmentManager,Fragment,Context)会被回调。
调用addObserver(LifecycleObserver)方法,使 Fragment 生命周期订阅类RxFragmentLifecycleObserver关联当前 Fragment 生命周期。

    @Override
    public void onFragmentAttached(@NonNull FragmentManager fm, @NonNull Fragment f, @NonNull Context context) {
        super.onFragmentAttached(fm, f, context);
        f.getLifecycle().addObserver(new RxFragmentLifecycleObserver(f));
    }

2.3、RxActivityStoreRxFragmentStore关联 Activity 或 Fragment 生命周期

RxActivityLifecycleObserverRxFragmentLifecycleObserver在 Activity 或 Fragment 创建之后(创建时完成依赖注入操作),响应Lifecycle.Event.ON_CREATE事件,获取 Activity 或 Fragment 中的 RxActivityStoreRxFragmentStore,并将其关联到 Activity 或 Fragment 生命周期。

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreate() {
        if (mActivity instanceof RxFluxView) {
            ViewModel rxStore = ((RxFluxView) mActivity).getRxStore();
            if (rxStore instanceof RxActivityStore) {
                //rxStore关联activity生命周期
                ((FragmentActivity) mActivity).getLifecycle().addObserver((RxActivityStore) rxStore);
            }
        }
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreate() {
        if (mFragment instanceof RxFluxView) {
            ViewModel rxStore = ((RxFluxView) mFragment).getRxStore();
            if (rxStore instanceof RxFragmentStore) {
                //rxStore关联Fragment生命周期
                mFragment.getLifecycle().addObserver((RxFragmentStore) rxStore);
            }
        }
    }

3、Store 注册订阅

RxActivityStoreRxFragmentStore继承ViewModel,实现LifecycleObserver,响应Lifecycle.Event.ON_CREATE事件,注册订阅,可以接收数据总线库core-eventbus 发送的通知。

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void subscribe() {
        if (mRxDispatcher.isSubscribe(this)) {
            return;
        }
        Log.i(RxFlux.TAG, "Subscribe RxFragmentStore : " + getClass().getSimpleName());
        mRxDispatcher.subscribeRxStore(this);
    }

4、View 注册订阅

RxFlux在 Activity 或 Fragment onResume状态时,使 Activity 或 Fragment 注册订阅,使其可以接收数据总线库core-eventbus发送的通知。

    @Override
    public void onActivityResumed(Activity activity) {
        if (activity instanceof RxSubscriberView) {
            if (mRxDispatcher.isSubscribe(this)) {
                return;
            }
            Log.i(TAG, "Subscribe RxFluxActivity : " + activity.getClass().getSimpleName());
            mRxDispatcher.subscribeRxView((RxSubscriberView) activity);
        }
    }

    @Override
    public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentResumed(fm, f);
        if (f instanceof RxSubscriberView) {
            if (mRxDispatcher.isSubscribe(this)) {
                return;
            }
            Log.i(TAG, "Subscribe RxFluxFragment : " + f.getClass().getSimpleName());
            mRxDispatcher.subscribeRxView((RxSubscriberView) f);
        }
    }

5、View 取消订阅

RxFlux在 Activity 或 Fragment onPause状态时,使 Activity 或 Fragment 取消订阅,使其不再接收数据总线库core-eventbus发送的通知。

    @Override
    public void onActivityPaused(Activity activity) {
        if (activity instanceof RxSubscriberView) {
            Log.i(TAG, "Unsubscribe RxFluxActivity : " + activity.getClass().getSimpleName());
            mRxDispatcher.unsubscribeRxView((RxSubscriberView) activity);
        }
    }

    @Override
    public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentPaused(fm, f);
        if (f instanceof RxSubscriberView) {
            Log.i(TAG, "Unsubscribe RxFluxFragment : " + f.getClass().getSimpleName());
            mRxDispatcher.unsubscribeRxView((RxSubscriberView) f);
        }
    }

6、Store 取消订阅

RxActivityStoreRxFragmentStore,响应Lifecycle.Event.ON_DESTROY事件,取消订阅,不再接收数据总线库core-eventbus发送的通知。

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void unsubscribe() {
        Log.i(RxFlux.TAG, "Unsubscribe RxFragmentStore : " + getClass().getSimpleName());
        mRxDispatcher.unsubscribeRxStore(this);
    }

源码

开源模块化解耦框架RxFluxArchitecture,欢迎大家点赞Fork,更欢迎点评指导。

推荐阅读更多精彩内容