Android 架构组件——Lifecycle

       首先介绍一下,这个架构是google官方推出的,主要也是为了代码规范,这个架构主要分为三个部分:Lifecycle、LiveData和ViewModle 共同组成了MVVM的模式。个人感觉其实和我们大家使用的MVP架构是一样的,不过在MVP的基础上又优化了一些,既然是官方推的,还是建议能使用的就使用吧。

       首先首先LifeCycle,Lifecycle组件包括LifecycleOwner、LifecycleObserver,LifecycleObserver其实类似于我们MVP中的Presenter了,LifecycleOwner就是我们的View层了。这个组件其实就是管理生命周期的,以前我们写MVP的时候会在Presenter的基础接口中定义一系列的生命周期方法(一般都是和Activity的生命周期对应),然后再View的基础接口中每个生命周期方法中一一对应起来,来达到管理生命周期的方法。而LifeCycle属于对这个进行了定义与管理,并且提供了一些默认的实现。

      1、看一下我们以前MVP实现生命周期管理的方法:

/**
 *Presenter接口
*/
public interface IPresenter {
    void onCreate();

    void onStart();

    void onResume();

    void onPause();

    void onStop();

    void onDestroy();
}

/**
 *Presenter实现类
*/
public class MainPresenter implements IPresenter {

    public MainPresenter(Context context){

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {

    }
}


/**
 *View 
*/
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private IPresenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        setContentView(R.layout.activity_main);
        mPresenter = new MainPresenter(this);
        mPresenter.onCreate();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
        mPresenter.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        mPresenter.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
        mPresenter.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
        mPresenter.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
        mPresenter.onDestroy();
    }
}

这个就很简单了,不说了。

2、下面看一下使用LifeCycle的方式:

(1)首先我们在buile.gradle中添加依赖:

    compile "android.arch.lifecycle:extensions:1.1.0"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
    compile "android.arch.lifecycle:runtime:1.1.0"

(2)创建我们自己的Presenter接口:

其中:lifecycle.Event为系统定义的事件枚举类(和Activity生命周期对应),LifecycleOwner 就是我们要监听的View。

onLifecycleChanged这个方法会监听到所有的事件,其实我们可以只写这一个方法,在里面做事件的判断即可。
public interface IPresenter extends LifecycleObserver{

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreate(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onStart(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPause(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onStop(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroy(@NotNull LifecycleOwner owner);

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    void onLifecycleChanged(@NotNull LifecycleOwner owner,
                            @NotNull Lifecycle.Event event);

}

(3)创建具体Preserter:

每个方法打印了一条日志。

public class MyPresenter implements IPresenter{

    private String TAG=MyPresenter.class.getSimpleName();

    @Override
    public void onCreate(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onCreate");
    }

    @Override
    public void onStart(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onStart");
    }

    @Override
    public void onResume(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onResume");
    }

    @Override
    public void onPause(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onPause");
    }

    @Override
    public void onStop(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onStop");
    }

    @Override
    public void onDestroy(@NotNull LifecycleOwner owner){
        Log.d(TAG,"onDestroy");
    }

    @Override
    public void onLifecycleChanged(@NotNull LifecycleOwner owner, @NotNull Lifecycle.Event event){
        Log.d(TAG,"onLifecycleChanged-->event:"+event);
    }

}

(4)将View层和Presenter进行关联:

其实就是将我们的Presenter添加为Activity的观察者。

public class MainActivity extends AppCompatActivity{

    private String TAG=MainActivity.class.getSimpleName();

    private MyPresenter myPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        setContentView(R.layout.activity_main);
        myPresenter=new MyPresenter();
        getLifecycle().addObserver(myPresenter);//添加LifecycleObserver
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}

(5)我们运行一下看下日志:

从图中可以看出来,在View出现过程是先调用View的生命周期方法,后调用的Presenter的对应方法,而在View销毁的过程是相反的。

09-30 11:28:59.796 5000-5000/? D/MainActivity: onCreate
09-30 11:01:14.933 26449-26449/? D/MyPresenter: onCreate
09-30 11:01:14.933 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_CREATE
09-30 11:01:14.933 26449-26449/? D/MainActivity: onStart
09-30 11:01:14.933 26449-26449/? D/MyPresenter: onStart
09-30 11:01:14.933 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_START
09-30 11:01:14.933 26449-26449/? D/MainActivity: onResume
09-30 11:01:14.934 26449-26449/? D/MyPresenter: onResume
09-30 11:01:14.934 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_RESUME
09-30 11:02:29.690 26449-26449/? D/MyPresenter: onPause
09-30 11:02:29.690 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_PAUSE
09-30 11:02:29.690 26449-26449/? D/MainActivity: onPause
09-30 11:02:29.793 26449-26449/? D/MyPresenter: onStop
09-30 11:02:29.793 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_STOP
09-30 11:02:29.794 26449-26449/? D/MainActivity: onStop
09-30 11:02:29.794 26449-26449/? D/MyPresenter: onDestroy
09-30 11:02:29.794 26449-26449/? D/MyPresenter: onLifecycleChanged-->event:ON_DESTROY
09-30 11:02:29.794 26449-26449/? D/MainActivity: onDestroy

(6)上面那个IPresenter是我们自己定义的,其他本身lifeCycle有几个默认的实现,一般情况我们直接集成它的子类即可。

Android 架构组件——Lifecycle_第1张图片

Android 架构组件——Lifecycle_第2张图片

3、这个lifeCycle其实很简单,这里写也是为了后面介绍Lifecycle、LiveData和ViewModle 共同组成了MVVM的模式做个铺垫。

参考文章:https://blog.csdn.net/zhuzp_blog/article/details/78871374

你可能感兴趣的:(Android,架构组件)