Lifecycle架构在MVP中的基本使用

依赖的选择:
// annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
// java8 使用这个依赖,minsdkversion 大于等于 24,否则编译不通过,区别与上面的是增加了 DefaultLifecyceObserver,不需要注释也可以绑定 LifecycleOwner 的生命周期上,使用交方便,不需要再去定义一个BaseLifecycleObserver接口
implementation "android.arch.lifecycle:common-java8:1.1.1"

/*
* java 8.0之前 minsdkversion24之前可用,需要注释实现生命周期观察
*/
public interface BaseLifecycleObserver extends LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event. ON_CREATE )
void onCrete();

@OnLifecycleEvent(Lifecycle.Event. ON_RESUME )
void onResume();

@OnLifecycleEvent(Lifecycle.Event. ON_PAUSE )
void onPause();

@OnLifecycleEvent(Lifecycle.Event. ON_STOP )
void onStop();

@OnLifecycleEvent(Lifecycle.Event. ON_DESTROY )
void onDestroy();
}

/**
* 实现类,适合minsdk 24之前
**/
public class BasePresenter implements BaseLifecycleObserver {
@Override
public void onCrete() {
Log. e ("===BasePresenter===","====onCrete====");
}

@Override
public void onResume() {
Log. e ("===BasePresenter===","=====onResume===");
}

@Override
public void onPause() {
Log. e ("===BasePresenter===","====onPause====");
}

@Override
public void onStop() {
Log. e ("===BasePresenter===","====onStop====");
}

@Override
public void onDestroy() {
Log. e ("===BasePresenter===","====onDestroy====");
}
}

/**
* 直接实现 DefaultLifecycleObserver 接口,必须用第二个依赖包,另外 minsdkversion 要大于24
* 通过标记的修改 可以在继承该 Basepersenter2 的类中得到当前的 LifecycleOwner处于什么状*
态,从而在对应状态下执行接口回调或者资源释放
*/
public class BasePresenter2 implements DefaultLifecycleObserver {
public static final int ON_CREATE = 1;
public static final int ON_START = 2;
public static final int ON_RESUME = 3;
public static final int ON_PAUSE = 4;
public static final int ON_STOP = 5;
public static final int ON_DESTROY = 6;
public int tag;
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","======onCreate====");
tag = ON_CREATE ;
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","=======onStart===");
tag = ON_START ;
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","=======onResume===");
tag = ON_RESUME ;
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","========onPause==");
tag = ON_PAUSE ;
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","=======onStop===");
tag = ON_STOP ;
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
Log. e ("===BasePresenter2===","==========");
tag = ON_DESTROY ;
}
}

/**
* 自定义的 Persenter 实现上面的BasePersenter ,从而监听到 LifecycleOwner的生命周期变化情况,只有当 activity 状态是 resume 时才执行回调,防止内存泄漏,这才是终极功能
*
**/
public class MainActivityPresenterImpl extends BasePresenter2 implements MainActivityPresenter{
MainActivityModelImpl mainPresenterModel;
MainActivityView mainActivityView;

public MainActivityPresenterImpl(MainActivityView mainActivityView) {
this.mainActivityView = mainActivityView;
mainPresenterModel = new MainActivityModelImpl();
}

@Override
public void loadImage(String url) {
mainPresenterModel.loadImage("aaa", "a", new BaseTaskCallBack() {
@Override
public void callBack(User user) {
//直接获取到当前 activity 的状态 tag
Log. e ("====loadImage====","===== 执行完异步任务回调 ====="+tag);
if (tag == ON_RESUME ) {
mainActivityView.setImage(user);
}else {
Log. e ("===loadImage====","===== 不处于可见状态,不执行回调 ====");
}
}
});
}


@Override
public void loadText(String text) {
mainPresenterModel.loadText("aaa", "aa", new BaseTaskCallBack() {
@Override
public void callBack(Object o) {
Log. e ("====loadText====","===== 执行完异步任务回调 ====="+tag);
if (tag == ON_RESUME ) {
mainActivityView.setText((String) o);
}else {
Log. e ("===loadText====","===== 不处于可见状态,不执行回调 ====");
}
}
});
}

@Override
public void loadData() {
mainPresenterModel.loadData(new BaseTaskCallBack>() {
@Override
public void callBack(List strings) {
Log. e ("====loadData====","===== 执行完异步任务回调 ====="+tag);
if (tag == ON_RESUME ) {
mainActivityView.setData(strings);
}else {
Log. e ("===loadData====","===== 不处于可见状态,不执行回调 ====");
}
}
});
}
}

/**
* activity 没什么讲的,只需要这一句话 getLifecycle.addObserver(LifecycleObserver);即可实现
*观察者与监听者的绑定。Activity可以不用实现 LifecycleOwner,从API14开始 AppCompatActivity已
*经实现了 LifecycleOwner
**/
pu blic class MainActivity extends AppCompatActivity implements MainActivityView ,LifecycleOwner{
MainActivityPresenterImpl mainActivityPresenter;
mainActivityPresenter = new MainActivityPresenterImpl(this);
getLifecycle().addObserver(mainActivityPresenter);
mainActivityPresenter.loadImage("11111");
mainActivityPresenter.loadText("22222");
mainActivityPresenter.loadData();
}

@Override
public void setImage(User s) {
Log. e ("===== 回调到结果 =====","=====setImage======"+s.toString());
}

@Override
public void setText(String text) {
Log. e ("===== 回调到结果 =====","======setText====="+text);
}

@Override
public void setData(List list) {
Log. e ("===== 回调到结果 =====","======setData====="+list);
}

你可能感兴趣的