LifeCyle的理解

产生的背景

先看个代码

class LifeCycleOldActivity : AppCompatActivity(){
    lateinit var presenter: Presenter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        presenter = Presenter()
        presenter.onCreate()
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroy()
    }
}

class Presenter{
    fun onCreate(){}
    fun onDestroy(){}
}

乍一看,很普通的逻辑,onCreate创建,onDestroy销毁,规范合理严谨。这是咱们祖辈总结出的模板性代码。但是这个有以下几个问题:

  1. 页面多了这种代码将到处都是
  2. Activity里太多P的代码,依赖度太高
  3. 执行顺序上不一定有保证,presenter.onDestroy不是一定会在presenter.onCreate后执行的,有可能出现onDestroy都走到了,onCreate后的一些方法还在执行
  4. 感知其它方法又要搞一大堆代码,乏味还不一定对

主要解决的痛点

核心还是优雅的去感知生命周期组件:

  • 将声明周期感知的逻辑从生命周期组件中分离出来,将感知对象作为生命周期组件的一个观察者就好
  • 感知自由度高且渐变,想感知啥的话注解加一下就好
  • LifeCycle内部处理严格,基于状态去处理,可以自由的根据状态去执行相应逻辑,确保生命周期执行的合理和安全性
class LifecycleNewActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //LifecycleNewActivity 实现了LifecycleOwner 是一个生命周期组件
        //添加对应的观察者
        lifecycle.addObserver(NewPresenter())
    }
    fun requestSuccess(){
        //可以确保此处执行的安全性
        if(lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)){
            //refresh 
        }
    }
}

/**
 * 生命周期组件的观察者,在这里感应生命周期变化就好
 */
class NewPresenter : LifecycleObserver{
    //通过注解来决定感知啥事件
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate(){
        
    }
}

核心类说明

LifeCycle
  • 三个核心方法
    1. addObserver:添加观察者
    2. removeObserver:移除观察者
    3. getCurrentState:获取当前状态
  • 两个枚举对象
    1. Event 事件
      ON_CREATE,
      ON_START,
      ON_RESUME,
      ON_PAUSE,
      ON_STOP,
      ON_DESTROY,
      ON_ANY
    2. State 状态
      • INITLALIZED 初始态:这是他被构造但还没收完成onCreate方法时的状态-->onCreate方法体状态
      • CREATED 创建态:在两种情况下会达到此状态:在onCreate调用之后;就在onStop调用之前-->onStart onStop 方法体状态
      • STARTED 启动态:在两种情况下会达到此状态:在onStart调用之后;就在onPause调用之前-->onResume onPause 方法体状态
      • RESUMED 可见态:调用onResume后达到此状态-->页面可见后onResume最后随便一个异步后就是此状态了
      • DESTORYED 销毁态:此状态后,将不再分发其它事件;在调用onDestroy方法前就会到达这个状态
LifecycleOwner

生命周期的拥有者,只有一个接口方法Lifecycle getLifecycle();实现了该接口的类,被定义为生命周期组件。

LifecycleObserver

生命周期组件的观察者,这是一个没有接口方法的接口,标识性接口,实现该接口的类在运行时通过解析OnLifecycleEvent注解达到指定生命周期事件的观察者的回调

LifecycleRegistry

系统提供的Lifecycle的实现,提供了如何感应生命周期变化的标准实践

源码段解析

以Activity为例解析生命周期感知的源码流程

//实现了LifecycleOwner
public class ComponentActivity ... implements LifecycleOwner  {
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        // 转接给ReportFragment去影响生命周期,这也是Glide影响生命周期的方案
        ReportFragment.injectIfNeededIn(this);
        ...
    }
}
public class ReportFragment extends android.app.Fragment {
   ... 
    public static void injectIfNeededIn(Activity activity) {
        if (Build.VERSION.SDK_INT >= 29) {
            // 29+版本 通过LifecycleCallbacks感知生命周期
            LifecycleCallbacks.registerIn(activity);
        }
          //通过添加个无页面Fragment感知生命周期
        android.app.FragmentManager manager = activity.getFragmentManager();
        if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
            manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
            manager.executePendingTransactions();
        }
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        ...
        //分发生命周期事件
        dispatch(Lifecycle.Event.ON_CREATE);
    }
static class LifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
        //onCreate执行后才会发送ON_CREATE事件
        @Override
        public void onActivityPostCreated(@NonNull Activity activity,
                @Nullable Bundle savedInstanceState) {
            dispatch(activity, Lifecycle.Event.ON_CREATE);
        }
}
    /**
     * 最终交由LifecycleRegistry的handleLifecycleEvent方法处理
     **/
    static void dispatch(@NonNull Activity activity, @NonNull Lifecycle.Event event) {
        if (activity instanceof LifecycleRegistryOwner) {
            ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
            return;
        }
        if (activity instanceof LifecycleOwner) {
            Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
            if (lifecycle instanceof LifecycleRegistry) {
                ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
            }
        }
    }
}
public class LifecycleRegistry extends Lifecycle {
  public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
      //确保主线程
      enforceMainThreadIfNeeded("handleLifecycleEvent");
      //根据当前事件将状态移动到对应状态
      moveToState(event.getTargetState());
    }

    public void addObserver(@NonNull LifecycleObserver observer) {
        ...
        //粘性事件的源头
        statefulObserver.dispatchEvent(lifecycleOwner, event);
        ...
    }

    //移动状态
  private void moveToState(State next) {
          //当前状态和目标状态一直,不继续了
          if (mState == next) {
              return;
          }
          //修改当前状态为目标状态
          mState = next;
          //正在处理事件,或者正在添加观察者暂不继续
          if (mHandlingEvent || mAddingObserverCounter != 0) {
              mNewEventOccurred = true;
              // we will figure out what to do on upper level.
              return;
          }
          //修改处理事件中标注为true
          mHandlingEvent = true;
          //同步去处理状态
          sync();
          //处理完后将标志改为false
          mHandlingEvent = false;
  }
  //同步生命周期状态
  private void sync() {
          LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
          //生命周期拥有者为空了,直接抛异常,一般出现在被系统回收后,异常的意思是,拥有者被回收了,改状态太晚了!
          if (lifecycleOwner == null) {
              throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is already"
                      + "garbage collected. It is too late to change lifecycle state.");
          }
          //还有未同步的观察者的话会无限循环去进行状态同步
          while (!isSynced()) {
              mNewEventOccurred = false;
              // no need to check eldest for nullability, because isSynced does it for us.
              if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
                  //向后同步
                  backwardPass(lifecycleOwner);
              }
              Map.Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
              if (!mNewEventOccurred && newest != null
                      && mState.compareTo(newest.getValue().mState) > 0) {
                  //向前同步
                  forwardPass(lifecycleOwner);
              }
          }
          mNewEventOccurred = false;
   }
  //true  都同步完了   false  还有未同步的
  private boolean isSynced() {
          if (mObserverMap.size() == 0) {
              return true;//木有观察者,按都是已经同步过的处理
          }
          State eldestObserverState = mObserverMap.eldest().getValue().mState;
          State newestObserverState = mObserverMap.newest().getValue().mState;
          return eldestObserverState == newestObserverState && mState == newestObserverState;
  }
  private void backwardPass(LifecycleOwner lifecycleOwner) {
          ...
          //根据状态映射回事件
          Event event = Event.downFrom(observer.mState);
          ...
          //想观察者发送事件
          observer.dispatchEvent(lifecycleOwner, event);
          ...
          }
  }

  static class ObserverWithState {
        ...
        //分发事件
        void dispatchEvent(LifecycleOwner owner, Event event) {
            ...
            mLifecycleObserver.onStateChanged(owner, event);
            ...
        }
    }
}
//onStateChanged的实现
class ReflectiveGenericLifecycleObserver implements LifecycleEventObserver {
    ...
    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Event event) {
        mInfo.invokeCallbacks(source, event, mWrapped);
    }
}
static class CallbackInfo {
        ...
        //通过反射调用LifecycleObserver被OnLifecycleEvent注解标注的方法
        @SuppressWarnings("ConstantConditions")
        void invokeCallbacks(LifecycleOwner source, Lifecycle.Event event, Object target) {
            invokeMethodsForEvent(mEventToHandlers.get(event), source, event, target);
            invokeMethodsForEvent(mEventToHandlers.get(Lifecycle.Event.ON_ANY), source, event,
                    target);
        }
}
//事件与状态的对象关系
public abstract class Lifecycle {
  ...
  public enum Event {
      //这个方法结合ReportFragment的dispatch的调用时机,完美的解释了,当回调某个生命周期方法后所对应的状态情况
      public State getTargetState() {
            switch (this) {
                case ON_CREATE: // 这个事件时在onActivityPostCreated发送的也就是onCreate执行完后
                case ON_STOP: // 这个事件时在onActivityPreStopped时发送的也就是onStop执行之前
                    return State.CREATED; //综合结论就是onCreate之后和onStop之间进入CREATED状态
                case ON_START:
                case ON_PAUSE:
                    return State.STARTED;
                case ON_RESUME:
                    return State.RESUMED;
                case ON_DESTROY:
                    return State.DESTROYED;
                case ON_ANY:
                    break;
            }
            throw new IllegalArgumentException(this + " has no target state");
        }
  }
  ...
}

推荐阅读更多精彩内容