Android开发Android开发经验谈Android技术知识

Android lifecycle 实战及使用进阶

2019-06-02  本文已影响42人  程序员徐公

前言

Android lifecycle 使用详解

Android LiveData 使用详解

Android lifecyle 源码解剖

Android livedata 源码解剖

Android lifecycle 实战及使用进阶

github sample 地址: ArchiteComponentsSample

Android Architecture Components 的,前面我们已经讲解了 lifecycle, LiveData 的使用及原理,今天,让我们一起赖看看 lifecycle 在项目中的实战。主要讲解两点

  1. LifecycleHandler 的封装
  2. MVP 中引入 lifecycle,可以自行监听 activity 生命周期变化

LifecycleHandle 的封装

我们平时在使用 handler 的时候,在 activity onDestroy 的时候,需要手动取 removeMessage 或者 removeCallBack,不然的话,很容易发生内存泄漏。

@Override
protected void onDestroy() {
    super.onDestroy();
    mHandler.removeCallbacksAndMessages(null);
}

这对我们开发者来说,很明显是一个重复的动作。我们一起通过 lifecycle 来减少这种重复性的动作,让其内部自己释放。

核心原理其实很简单,内部监听 activity 生命周期的变化,在 onDestroy 的时候移除 message 和 callback。

public class LifecycleHandler extends Handler implements FullLifecycleObserver {


    private LifecycleOwner mLifecycleOwner;

    public LifecycleHandler(LifecycleOwner lifecycleOwner) {
        mLifecycleOwner = lifecycleOwner;
        addObserver();
    }

    public LifecycleHandler(LifecycleOwner lifecycleOwner, Callback callback) {
        super(callback);
        mLifecycleOwner = lifecycleOwner;
        addObserver();
    }

    public LifecycleHandler(LifecycleOwner lifecycleOwner, Looper looper) {
        super(looper);
        mLifecycleOwner = lifecycleOwner;
        addObserver();
    }

    public LifecycleHandler(LifecycleOwner lifecycleOwner, Looper looper, Callback callback) {
        super(looper, callback);
        mLifecycleOwner = lifecycleOwner;
        addObserver();
    }

    private void addObserver() {
        if (mLifecycleOwner != null) {
            mLifecycleOwner.getLifecycle().addObserver(new FullLifecycleObserverAstrong textdapter(mLifecycleOwner, this));
        }
    }


    @Override
    public void onCreate(LifecycleOwner owner) {

    }

    @Override
    public void onStart(LifecycleOwner owner) {

    }

    @Override
    public void onResume(LifecycleOwner owner) {

    }

    @Override
    public void onPause(LifecycleOwner owner) {

    }

    @Override
    public void onStop(LifecycleOwner owner) {

    }

    @Override
    public void onDestroy(LifecycleOwner owner) {
        removeCallbacksAndMessages(null);
    }
}

我们来看一下这一行代码

mLifecycleOwner.getLifecycle().addObserver(new FullLifecycleObserverAdapter(mLifecycleOwner, this));

FullLifecycleObserverAdapter 是什么呢?

FullLifecycleObserverAdapter

public class FullLifecycleObserverAdapter implements LifecycleObserver {

    private final FullLifecycleObserver mObserver;

    private static final String TAG = "FullLifecycleObserverAd";
    private final LifecycleOwner mLifecycleOwner;

    FullLifecycleObserverAdapter(LifecycleOwner lifecycleOwner, FullLifecycleObserver observer) {
        mLifecycleOwner = lifecycleOwner;
        mObserver = observer;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        Log.i(TAG, "onCreate: ");
        mObserver.onCreate(mLifecycleOwner);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        Log.i(TAG, "onStart: ");
        mObserver.onStart(mLifecycleOwner);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        Log.i(TAG, "onResume: ");
        mObserver.onResume(mLifecycleOwner);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        Log.i(TAG, "onPause: ");
        mObserver.onPause(mLifecycleOwner);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
        Log.i(TAG, "onStop: ");
        mObserver.onStop(mLifecycleOwner);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        mObserver.onDestroy(mLifecycleOwner);
    }

}

FullLifecycleObserver

public interface FullLifecycleObserver extends LifecycleObserver {

    void onCreate(LifecycleOwner owner);

    void onStart(LifecycleOwner owner);

    void onResume(LifecycleOwner owner);

    void onPause(LifecycleOwner owner);

    void onStop(LifecycleOwner owner);

    void onDestroy(LifecycleOwner owner);
}

FullLifecycleObserver 是我们自己自定义的一个接口,继承 LifecycleObserver,各个方法一一对应 activity 的生命周期。

FullLifecycleObserverAdapter 实现了 LifecycleObserver,使用了 @OnLifecycleEvent 注解,在 activity 生命周期变化的时候会回调相应的方法,我们再在里面回调 observer 的 onCreate, onStart 等方法,这有点类似代理模式。调用关系总结如下:

enter description here

以后想观察 lifecycle 生命周期变化的话,只需要 mLifecycleOwner.getLifecycle().addObserver(new FullLifecycleObserverAdapter(mLifecycleOwner, this)); 这一行代码即可

有人可能会这样问了,为什么要这样设计呢

加 FullLifecycleObserver 这个接口,主要是为了跟 activity 的生命周期一一对应。


LifecyclePresenter 的封装

public class LifecyclePresenter implements IPresenter, FullLifecycleObserver {

    private static final String TAG = "BasePresenter";

    private final LifecycleOwner mLifecycleOwner;

    public LifecyclePresenter(LifecycleOwner lifecycleOwner) {
        mLifecycleOwner = lifecycleOwner;
        mLifecycleOwner.getLifecycle().addObserver(new FullLifecycleObserverAdapter(lifecycleOwner, this));
    }

    @Override
    public void onCreate(LifecycleOwner owner) {
        Log.i(TAG, "onCreate: owner = " + owner);
    }

    @Override
    public void onStart(LifecycleOwner owner) {
        Log.i(TAG, "onStart: owner = " + owner);
    }

    @Override
    public void onResume(LifecycleOwner owner) {
        Log.i(TAG, "onResume: owner = " + owner);
    }

    @Override
    public void onPause(LifecycleOwner owner) {
        Log.i(TAG, "onPause: owner = " + owner);
    }

    @Override
    public void onStop(LifecycleOwner owner) {
        Log.i(TAG, "onStop: owner = " + owner);
    }

    @Override
    public void onDestroy(LifecycleOwner owner) {
        Log.i(TAG, "onDestroy: owner = " + owner);
    }
}

Android 技术人,一位不羁的码农。

Android 技术人
上一篇下一篇

猜你喜欢

热点阅读