Android Jetpack系列--1.Lifecycle使用及源码解析

Jetpack简介

定义

  • Jetpack 是一个由多个库组成的套件;
  • 主要包括架构(Architecture)、基础(Foundation)、行为(Behavior) 、界面(UI)四个方面;

特点

  1. 提高开发效率和应用质量,具有向后兼容性,可以减少崩溃和内存泄露,让开发者可以更专心于写真正重要的代码;
  2. 消除样板代码,管理各种繁琐的 Activity(如后台任务、导航和生命周期管理);

AAC

  • Jetpack的精华主要是Architecture,全称是Android Architecture Component(AAC), 即Android架构组件;
  • 包括:DataBinding,Lifecycle,LiveData,ViewModel,Navigation,Paging,Room,WorkManager等;

Lifecycle

  • 用于帮助开发者管理Activity和Fragment 的生命周期,它是LiveData和ViewModel的基础;

引入依赖

  1. 非androidX项目:
implementation "android.arch.lifecycle:extensions:1.1.1"
  1. androidX项目
implementation 'androidx.appcompat:appcompat:1.2.0'
// appcompat依赖了androidx.fragment,而androidx.fragment下依赖了ViewModel和LiveData,LiveData又依赖了Lifecycle;
  1. 单独引入依赖
//根目录的 build.gradle
    repositories {
        google()
        ...
    }

//app的build.gradle
dependencies {
    def lifecycle_version = "2.2.0"
    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // 只有Lifecycles (不带 ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
    // lifecycle注解处理器
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    // 替换 - 如果使用Java8,就用这个替换上面的lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    //以下按需引入
    // 可选 - 帮助实现Service的LifecycleOwner
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
    // 可选 - ProcessLifecycleOwner给整个 app进程 提供一个lifecycle
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
    // 可选 - ReactiveStreams support for LiveData
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
    // 可选 - Test helpers for LiveData
    testImplementation "androidx.arch.core:core-testing:lifecycle_version"
}
  • 实际上如果只使用Lifecycle,只需要引入lifecycle-runtime即可;

基本使用

  • 在Activity中监听生命周期
class LifecycleDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle_demo)
        lifecycle.addObserver(MyObserver())
        LjyLogUtil.d("onCreate:主线程id=${mainLooper.thread.id}")
        GlobalScope.launch {
            val result1 = GlobalScope.async {
                delay(1000)
                1
            }
            val result2 = GlobalScope.async {
                delay(4000)
                4
            }
            val result = result1.await() + result2.await()
            LjyLogUtil.d("onCreate:协程线程id=${Thread.currentThread().id},result = $result")
        }
    }
}

class MyObserver : LifecycleObserver {
    //观察者的方法可以接受一个参数LifecycleOwner,就可以用来获取当前状态、或者继续添加观察者。
    //若注解的是ON_ANY还可以接收Event,用于区分是哪个事件。
    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
        LjyLogUtil.d("LifecycleObserver.onAny:owner=" + owner.javaClass.name + ",event.name=" + event.name)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    //fun onCreate() {
    fun onCreate(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onCreate:owner=${owner.javaClass.name}")
        GlobalScope.launch {
            delay(5000)
            //回调后检查当前生命周期状态
            if (owner.lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
                LjyLogUtil.d("LifecycleObserver.onCreate:协程线程id=${Thread.currentThread().id}")
            }
        }
    }
}
  • 实际开发中,如MVP架构可以让Presenter实现LifecycleObserver接口,在相应的方法上注解要触发的生命周期,再在Activity中作为观察者添加到Lifecycle中,这样Presenter就可以自动感知Activity生命周期并执行方法;
  • 如上面代码所示,可以回调后检查当前生命周期状态,从而避免内存泄露问题

自定义LifecycleOwner

  • 实现自定义LifecycleOwner,可以使用LifecycleRegistry,它是Lifecycle的实现类;
  • 由下面代码也可以看出各生命周期方法的执行与生命周期状态的关系;
class MainActivity : Activity(), LifecycleOwner {
    private lateinit var mLifecycleRegistry: LifecycleRegistry

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mLifecycleRegistry = LifecycleRegistry(this)
        mLifecycleRegistry.currentState = Lifecycle.State.CREATED
        lifecycle.addObserver(MyObserver2())

        //startActivity(Intent(this, LifecycleDemoActivity::class.java))
    }

    override fun onStart() {
        super.onStart()
        mLifecycleRegistry.currentState = Lifecycle.State.STARTED
    }

    override fun onResume() {
        super.onResume()
        mLifecycleRegistry.currentState = Lifecycle.State.RESUMED
    }

    override fun onPause() {
        super.onPause()
        mLifecycleRegistry.currentState = Lifecycle.State.STARTED
    }

    override fun onStop() {
        super.onStop()
        mLifecycleRegistry.currentState = Lifecycle.State.CREATED
    }

    override fun onDestroy() {
        super.onDestroy()
        mLifecycleRegistry.currentState = Lifecycle.State.DESTROYED
    }

    override fun getLifecycle(): Lifecycle {
        return mLifecycleRegistry
    }
}

class MyObserver2 : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onCreate:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onStart:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onResume:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onPause:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onStop:${owner.lifecycle.currentState}")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onDestroy:${owner.lifecycle.currentState}")
    }
}

ProcessLifecycleOwner

  • 监听Application生命周期,使用方式和Activity中类似;
  • 使用ProcessLifecycleOwner.get()获取实例;

示例:App进入前后台的判断

  • 之前可以通过registerActivityLifecycleCallbacks(callback)方法,在callback中利用一个全局变量做计数,
    onActivityStarted()时 +1,onActivityStopped时 -1,以此判断前后台切换。
  • 而使用ProcessLifecycleOwner可以直接获取应用前后台切换状态。
//1.引入依赖
implementation "androidx.lifecycle:lifecycle-process:2.3.1"
//2.实现代码
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationLifecycleObserver())
    }
}

class ApplicationLifecycleObserver :LifecycleObserver{
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onAppForeground(owner: LifecycleOwner){
        LjyLogUtil.d("${owner.javaClass.simpleName}:app moved to foreground")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onAppBackground(owner: LifecycleOwner){
        LjyLogUtil.d("${owner.javaClass.simpleName}:app moved to background")
    }
}

Lifecycle原理及源码分析

Lifecycle类

  • 开发中我们直接使用的就是Lifecycle类,所以先来看看它是如何实现的
public abstract class Lifecycle {

    //添加观察者
    @MainThread
    public abstract void addObserver(@NonNull LifecycleObserver observer);

    //移除观察者
    @MainThread
    public abstract void removeObserver(@NonNull LifecycleObserver observer);

    //获取状态
    @MainThread
    @NonNull
    public abstract State getCurrentState();

    //生命周期事件
    @SuppressWarnings("WeakerAccess")
    public enum Event {
        ON_CREATE,
        ON_START,
        ON_RESUME,
        ON_PAUSE,
        ON_STOP,
        ON_DESTROY,
        ON_ANY //响应任意事件
    }

    //生命周期状态
    @SuppressWarnings("WeakerAccess")
    public enum State {
        DESTROYED,
        INITIALIZED,
        CREATED,
        STARTED,
        RESUMED;

        //判断至少是某一状态
        public boolean isAtLeast(@NonNull State state) {
            return compareTo(state) >= 0;
        }
    }
}

Lifecycle如何观察Activity和Fragment的生命周期

  • 在Android Support Library 26.1.0 及其之后的版本,Activity和Fragment已经默认实现了LifecycleOwner接口,LifecycleOwner可以理解为被观察者;
ComponentActivity
  • 之前的代码中有调用ComponentActivity.getLifecycle()来获取Lifecycle实例,那么可以推测Lifecycle与ComponentActivity是在ComponentActivity中进行了某些操作,让我们来看看ComponentActivity的代码,代码中可以看到ComponentActivity实现了接口LifecycleOwner,并在getLifecycle()返回了LifecycleRegistry实例,与我们之前的自定义LifecycleOwner代码是何其相似;
@RestrictTo(LIBRARY_GROUP)
public class ComponentActivity extends Activity implements LifecycleOwner {
    private SimpleArrayMap, ExtraData> mExtraDataMap =
            new SimpleArrayMap<>();

    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);//1

    @RestrictTo(LIBRARY_GROUP)
    public void putExtraData(ExtraData extraData) {
        mExtraDataMap.put(extraData.getClass(), extraData);
    }

    @Override
    @SuppressWarnings("RestrictedApi")
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ReportFragment.injectIfNeededIn(this);//2
    }

    @CallSuper
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mLifecycleRegistry.markState(Lifecycle.State.CREATED);//3
        super.onSaveInstanceState(outState);
    }

    @RestrictTo(LIBRARY_GROUP)
    public  T getExtraData(Class extraDataClass) {
        return (T) mExtraDataMap.get(extraDataClass);
    }

    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;//4
    }

    @RestrictTo(LIBRARY_GROUP)
    public static class ExtraData {
    }
}
  • 但是我们看到其只在onSaveInstanceState中调用了mLifecycleRegistry.markState(Lifecycle.State.CREATED),而并没有向我们的例子那样在各生命周期中改变Lifecycle状态,
  • 在其onCreate中有调用 ReportFragment.injectIfNeededIn(this),我们再继续看看ReportFragment中是如何实现的
ReportFragment
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public class ReportFragment extends android.app.Fragment {

    public static void injectIfNeededIn(Activity activity) {
        if (Build.VERSION.SDK_INT >= 29) {
            LifecycleCallbacks.registerIn(activity);
        }
        android.app.FragmentManager manager = activity.getFragmentManager();
        if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
            manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
            manager.executePendingTransactions();
        }
    }

    @SuppressWarnings("deprecation")
    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);
            }
        }
    }

    static ReportFragment get(Activity activity) {
        return (ReportFragment) activity.getFragmentManager().findFragmentByTag(
                REPORT_FRAGMENT_TAG);
    }

    private ActivityInitializationListener mProcessListener;

    private void dispatchCreate(ActivityInitializationListener listener) {
        if (listener != null) {
            listener.onCreate();
        }
    }

    private void dispatchStart(ActivityInitializationListener listener) {
        if (listener != null) {
            listener.onStart();
        }
    }

    private void dispatchResume(ActivityInitializationListener listener) {
        if (listener != null) {
            listener.onResume();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dispatchCreate(mProcessListener);
        dispatch(Lifecycle.Event.ON_CREATE);
    }

    @Override
    public void onStart() {
        super.onStart();
        dispatchStart(mProcessListener);
        dispatch(Lifecycle.Event.ON_START);
    }

    @Override
    public void onResume() {
        super.onResume();
        dispatchResume(mProcessListener);
        dispatch(Lifecycle.Event.ON_RESUME);
    }

    @Override
    public void onPause() {
        super.onPause();
        dispatch(Lifecycle.Event.ON_PAUSE);
    }

    @Override
    public void onStop() {
        super.onStop();
        dispatch(Lifecycle.Event.ON_STOP);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dispatch(Lifecycle.Event.ON_DESTROY);
        // just want to be sure that we won't leak reference to an activity
        mProcessListener = null;
    }

    private void dispatch(@NonNull Lifecycle.Event event) {
        if (Build.VERSION.SDK_INT < 29) {
            dispatch(getActivity(), event);
        }
    }

    void setProcessListener(ActivityInitializationListener processListener) {
        mProcessListener = processListener;
    }

    interface ActivityInitializationListener {
        void onCreate();

        void onStart();

        void onResume();
    }
   ...
}
  • 从上面代码可以看到其通过dispatch方法改变Lifecycle状态,而dispatch中又调用了LifecycleRegistry.handleLifecycleEvent()
LifecycleRegistry
  • LifecycleRegistry中部分代码如下,handleLifecycleEvent中调用了moveToState();
  • moveToState()负责移动到新状态,最后使用sync()把生命周期状态同步给所有观察者
  • sync()中会根据当前状态和mObserverMap中的eldest和newest的状态做对比 ,判断当前状态是向前(backwardPass)还是向后(backwardPass)
  • backwardPass()和backwardPass()中都调用了其静态内部类ObserverWithState.dispatchEvent()
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
    enforceMainThreadIfNeeded("handleLifecycleEvent");
    moveToState(event.getTargetState());
}

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;
    }
    mHandlingEvent = true;
    sync();
    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.");
    }
    //遍历观察者,观察者存放在mObserverMap中,mObserverMap对观察者的添加是 Activity中使用getLifecycle().addObserver()
    //循环条件是!isSynced(),若最老的和最新的观察者的状态一致,且都是ower的当前状态,说明已经同步完了
    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 newest = mObserverMap.newest();
        if (!mNewEventOccurred && newest != null
                && mState.compareTo(newest.getValue().mState) > 0) {
            forwardPass(lifecycleOwner);
        }
    }
    mNewEventOccurred = false;
}

private void forwardPass(LifecycleOwner lifecycleOwner) {
    Iterator> ascendingIterator =
            mObserverMap.iteratorWithAdditions();
    while (ascendingIterator.hasNext() && !mNewEventOccurred) {
        Map.Entry entry = ascendingIterator.next();
        ObserverWithState observer = entry.getValue();
        while ((observer.mState.compareTo(mState) < 0 && !mNewEventOccurred
                && mObserverMap.contains(entry.getKey()))) {
            pushParentState(observer.mState);
            final Event event = Event.upFrom(observer.mState);
            if (event == null) {
                throw new IllegalStateException("no event up from " + observer.mState);
            }
            observer.dispatchEvent(lifecycleOwner, event);
            popParentState();
        }
    }
}

private void backwardPass(LifecycleOwner lifecycleOwner) {
    Iterator> descendingIterator =
            mObserverMap.descendingIterator();
    while (descendingIterator.hasNext() && !mNewEventOccurred) {
        Map.Entry entry = descendingIterator.next();
        ObserverWithState observer = entry.getValue();
        while ((observer.mState.compareTo(mState) > 0 && !mNewEventOccurred
                && mObserverMap.contains(entry.getKey()))) {
            Event event = Event.downFrom(observer.mState);
            if (event == null) {
                throw new IllegalStateException("no event down from " + observer.mState);
            }
            pushParentState(event.getTargetState());
            observer.dispatchEvent(lifecycleOwner, event);
            popParentState();
        }
    }
}

static class ObserverWithState {
    State mState;
    LifecycleEventObserver mLifecycleObserver;

    ObserverWithState(LifecycleObserver observer, State initialState) {
        mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer);
        mState = initialState;
    }

    void dispatchEvent(LifecycleOwner owner, Event event) {
        State newState = event.getTargetState();
        mState = min(mState, newState);
        mLifecycleObserver.onStateChanged(owner, event);
        mState = newState;
    }
}
ReflectiveGenericLifecycleObserver
  • 上面的dispatchEvent()中又调用了mLifecycleObserver.onStateChanged(),其代码如下
class ReflectiveGenericLifecycleObserver implements LifecycleEventObserver {
    private final Object mWrapped;
    private final CallbackInfo mInfo;

    ReflectiveGenericLifecycleObserver(Object wrapped) {
        mWrapped = wrapped;
        mInfo = ClassesInfoCache.sInstance.getInfo(mWrapped.getClass());
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Event event) {
        mInfo.invokeCallbacks(source, event, mWrapped);
    }
}
CallbackInfo
  • 上面的onStateChanged()中又调用了 mInfo.invokeCallbacks,其代码如下
static class CallbackInfo {
    final Map> mEventToHandlers;
    final Map mHandlerToEvent;

    CallbackInfo(Map handlerToEvent) {
        mHandlerToEvent = handlerToEvent;
        mEventToHandlers = new HashMap<>();
        for (Map.Entry entry : handlerToEvent.entrySet()) {
            Lifecycle.Event event = entry.getValue();
            List methodReferences = mEventToHandlers.get(event);
            if (methodReferences == null) {
                methodReferences = new ArrayList<>();
                mEventToHandlers.put(event, methodReferences);
            }
            methodReferences.add(entry.getKey());
        }
    }

    @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);
    }

    private static void invokeMethodsForEvent(List handlers,
            LifecycleOwner source, Lifecycle.Event event, Object mWrapped) {
        if (handlers != null) {
            for (int i = handlers.size() - 1; i >= 0; i--) {
                handlers.get(i).invokeCallback(source, event, mWrapped);
            }
        }
    }
}
MethodReference
  • invokeCallbacks()中又间接调用了MethodReference.invokeCallback,其代码如下
static final class MethodReference {
    final int mCallType;
    final Method mMethod;

    MethodReference(int callType, Method method) {
        mCallType = callType;
        mMethod = method;
        mMethod.setAccessible(true);
    }

    void invokeCallback(LifecycleOwner source, Lifecycle.Event event, Object target) {
        //noinspection TryWithIdenticalCatches
        try {
            switch (mCallType) {
                case CALL_TYPE_NO_ARG:
                    mMethod.invoke(target);
                    break;
                case CALL_TYPE_PROVIDER:
                    mMethod.invoke(target, source);
                    break;
                case CALL_TYPE_PROVIDER_WITH_EVENT:
                    mMethod.invoke(target, source, event);
                    break;
            }
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Failed to call observer method", e.getCause());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
  • 所以总结来说,实现LifecycleObserver接口的类中,注解修饰的方法和事件会被保存起来,最后通过反射对事件的对应方法进行调用

我是今阳,如果想要进阶和了解更多的干货,欢迎关注微信公众号 “今阳说” 接收我的最新文章

你可能感兴趣的:(Android Jetpack系列--1.Lifecycle使用及源码解析)