美文网首页DevSupport随笔
android lifecycle介绍及实践

android lifecycle介绍及实践

作者: 成虫_62d0 | 来源:发表于2019-09-17 14:49 被阅读0次
    • 介绍
      Lifecycle 使得其它组件可以获取类似activity和Fragment那样响应生命周期的能力。我们可以直接在fragment和activity的相关生命周期函数中进行操作,但是这样的代码不太好维护,耦合性太高。 而生lifecycle-aware组件利用观察者模式,使得其它对“生命周期”感兴趣的组件能自己处理,而不必直接在fragment和activity中进行处理。

    • 背景
      最近项目组要启动一个新项目,以前的项目都是使用简单的mvp架构。其中有一个繁琐的处理就是,p层和view层强耦合,原因就在于生命周期的处理方式。现在全新启动一个项目,完全没有技术负担,所以对这个问题使用lifecycle-aware组件进行优化。

    • 依赖

    allprojects {
        repositories {
            jcenter()
            google()//添加Google Maven仓库
        }
    }
    
    dependencies {
        // Java8 support for Lifecycles
        implementation "android.arch.lifecycle:common-java8:1.1.1"
    // Lifecycles only (no ViewModel or LiveData)
        implementation "android.arch.lifecycle:runtime:1.1.1"
        annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
    }
    
    • Presenter层实现

    懒加载fragment相关的生命周期接口

    public interface CoinSignalLifecycleObserver extends DefaultLifecycleObserver {
    
        default void onFragmentFirstVisible(){}
    
        default void onFragmentResume(){}
    
        default void onFragmentPause(){}
    
    }
    

    presenter接口

    public interface BasePresenter extends CoinSignalLifecycleObserver {
    
        default void disPose(Disposable disposable) {
            if (disposable != null && !disposable.isDisposed()) {
                disposable.dispose();
            }
        }
    }
    

    fragment的实现

    public class BaseMvpFragment<T extends BasePresenter> extends Fragment{
    
        protected T presenter;
    
        protected void initPresenter(){}
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initPresenter();
            if (presenter != null) {
                getLifecycle().addObserver(presenter);
            }
        }
    }
    

    Activity的实现,其中activity里有沉浸式相关代码

    public abstract class BaseMvpActivity<T extends BasePresenter> extends AppCompatActivity implements LifecycleOwner {
        protected LifecycleRegistry mLifecycleRegistry;
        protected T presenter;
    
        protected ImmersiveHelper immersiveHelper;
        protected View statusView;
    
        protected abstract void initPresenter();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mLifecycleRegistry = new LifecycleRegistry(this);
            if (presenter != null) {
                getLifecycle().addObserver(presenter);
            }
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
            setLayout();
            statusView = findViewById(R.id.status_bar_view);
            if (statusView != null) {
                immersiveHelper = new ImmersiveHelper(this, statusView);
            }
        }
    
        protected abstract void setLayout();
    
        @Override
        protected void onStart() {
            super.onStart();
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
            if (immersiveHelper == null){
                return;
            }
            immersiveHelper.destroy();
            immersiveHelper = null;
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
            MobclickAgent.onResume(this);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
            MobclickAgent.onPause(this);
        }
    
        /**
         * 是否可以使用沉浸式
         * Is immersion bar enabled boolean.
         *
         * @return the boolean
         */
        public boolean isImmersionBarEnabled() {
            return immersiveHelper.isImmersionBarEnabled();
        }
    
        public void initImmersionBar() {
            //在BaseActivity里初始化
            immersiveHelper.initImmersionBar();
        }
    
        public void hideSoftKeyBoard() {
            if (immersiveHelper == null){
                return;
            }
            immersiveHelper.hideSoftKeyBoard();
        }
    
        public void finish() {
            super.finish();
            hideSoftKeyBoard();
        }
    }
    
    • 总结
      如上代码,在实现p层和view层相关代码的时候,它们俩个层次的耦合变得很低。

    相关文章

      网友评论

        本文标题:android lifecycle介绍及实践

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