Jetpack核心架构组件源码解析系列(1):Lifecycle运用与原理及源码分析

前言

Jetpack 是一个由多个库组成的套件,可帮助开发者遵循最佳做法,减少样板代码。如果项目采用 MVVM 架构,那么 Jetpack 里面的架构相关的组件就是为MVVM 量身定制的,而且现在面试Jetpack也是必问项,但是很多开发者对Jetpack中的一些核心组件都只停留在会用的阶段,对其原理及源码却是一知半解甚至根本没了解过,因此整理了一个系列来帮助有需要的小伙伴理解Jetpack设计思想和原理

正文

什么是Lifecycle?Lifecycle解决了开发中的什么问题?

我们先来一段Lifecycle出现之前的Activity生命周期监听代码:

1、先定义一个接口:

package com.kuer.ke_livedata;

public interface LifecycleInterface {
    void onCreate();

    void onResume();

    void onStart();

    void onStop();

    void onDestroy();
    //...
}

2、定义实现类

package com.kuer.ke_livedata;

import android.util.Log;

public class LifecycleImpl implements LifecycleInterface {
    @Override
    public void onCreate() {
        Log.e("LifecycleImpl------>","onCreate");
    }

    @Override
    public void onResume() {
        Log.e("LifecycleImpl------>","onResume");
    }

    @Override
    public void onStart() {
        Log.e("LifecycleImpl------>","onStart");
    }

    @Override
    public void onStop() {
        Log.e("LifecycleImpl------>","onStop");
    }

    @Override
    public void onDestroy() {
        Log.e("LifecycleImpl------>","onDestroy");
    }
    
    //...
}

3、生命周期监听(绑定)

package com.kuer.ke_livedata;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends AppCompatActivity {
    LifecycleImpl lifecycle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lifecycle = new LifecycleImpl();
        lifecycle.onCreate();
    }


    @Override
    protected void onResume() {
        super.onResume();
        lifecycle.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        lifecycle.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        lifecycle.onDestroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
        lifecycle.onStop();
    }
}

从以上代码可以看出,在以前监听Activity的生命周期变化是一件很麻烦的事情,而且如果逻辑复杂的话会显得代码特别的臃肿,耦合非常严重。

我们再来一段基于Lifecycle的Activity生命周期监听代码:

1、定义一个实现类实现LifecycleOwner接口(GenericLifecycleObserver是LifecycleOwner的子类,因为LifecycleOwner是空的,所以一般都是实现GenericLifecycleObserver)

package com.kuer.ke_livedata;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.lifecycle.GenericLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

@SuppressLint("RestrictedApi")
public class Feel implements GenericLifecycleObserver {

    //每当组件的生命周期发生改变的时候都会进行回调
    @Override
    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
        Log.e("Feel----------->",event.name());
    }
}

2、将这个接口作为观察者通过Lifecycle与Activity绑定起来

package com.kuer.ke_livedata;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.this.getLifecycle().addObserver(new Feel());
    }
}

简单的代码就能通过Feel对象监听MainActivity的生命周期变化,而且所有的处理逻辑不需要写在MainActivity中。Lifecycle中有多种观察者写法,这里就不一一例举了。

概念总结: Lifecycle其实就是一个生命周期中转站,当组件的生命周期发生改变时,会通知该组件所持有的Lifecycle(Lifecycle中采取了观察者模式,可以添加观察者),Lifecycle又会通知所有的观察者,观察者就可以做出相对应的逻辑处理。在整个过程中,观察者与MainActivity的耦合很低,而且不会给MainActivity造成多余的负担,达到了一个很好的解耦和瘦身的效果

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;
        //对比的方法 比如:State.RESUMED.isAtLeast(State.CREATED),
        //对比State.RESUMED的序数值是否大于State.CREATED的序数值(序数值不懂百度)
        public boolean isAtLeast(@NonNull State state) {
            return compareTo(state) >= 0;
        }
    }
}

通过源码我们可以看出,Lifecycle是一个抽象类,其内部不仅包括了添加和移除观察者的方法,还包括了EventState枚举,这两个枚举中Event指向组件生命周期对应的事件,这些事件会映射到 ActivityFragment中的事件分发,而State是用来配合Event来保存组件的生命周期执行轨迹的(这个概念会在下面的状态机中详细讲解)。

既然Lifecycle是一个抽象类,那么肯定会提供有具体的实现,它的实现类就是LifecycleRegistry,以下是LifecycleRegistry的源码:

public class LifecycleRegistry extends Lifecycle {

    private static final String LOG_TAG = "LifecycleRegistry";

    //所有观察者的容器
    private FastSafeIterableMap mObserverMap =
            new FastSafeIterableMap();
   
   //当前状态
    private State mState;

    private final WeakReference mLifecycleOwner;

    private int mAddingObserverCounter = 0;

    private boolean mHandlingEvent = false;
    private boolean mNewEventOccurred = false;

   
    private ArrayList mParentStates = new ArrayList();

   
    public LifecycleRegistry(@NonNull LifecycleOwner provider) {
        mLifecycleOwner = new WeakReference(provider);
        //当LifecycleRegistry对象被创建的好 初始化状态
        mState = INITIALIZED;
    }

    //改变状态的方法1  
    @SuppressWarnings("WeakerAccess")
    @MainThread
    public void markState(@NonNull State state) {
        moveToState(state);
    }

    //改变状态的方法2 
    public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
        State next = getStateAfter(event);
        moveToState(next);
    }

    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 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 State calculateTargetState(LifecycleObserver observer) {
        Entry previous = mObserverMap.ceil(observer);

        State siblingState = previous != null ? previous.getValue().mState : null;
        State parentState = !mParentStates.isEmpty() ? mParentStates.get(mParentStates.size() - 1)
                : null;
        return min(min(mState, siblingState), parentState);
    }

    //添加观察者的方法
    @Override
    public void addObserver(@NonNull LifecycleObserver observer) {
        State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
        //将观察者和Lifecycle的状态进行包装,方便观察者回调
        ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
        //将包装类放入map容器中
        ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);

        if (previous != null) {
            return;
        }
        LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
        if (lifecycleOwner == null) {
            // it is null we should be destroyed. Fallback quickly
            return;
        }

        boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
        State targetState = calculateTargetState(observer);
        mAddingObserverCounter++;
        while ((statefulObserver.mState.compareTo(targetState) > ascendingIterator =
                mObserverMap.iteratorWithAdditions();
        while (ascendingIterator.hasNext() && !mNewEventOccurred) {
            //将数组的Entry拿出来,Entry中key就是观察者,Value就是Lifecycle的状态
            Entry entry = ascendingIterator.next();
            ObserverWithState observer = entry.getValue();
            while ((observer.mState.compareTo(mState) > descendingIterator =
                mObserverMap.descendingIterator();
        while (descendingIterator.hasNext() && !mNewEventOccurred) {
            Entry entry = descendingIterator.next();
            ObserverWithState observer = entry.getValue();
            while ((observer.mState.compareTo(mState) > 0 && !mNewEventOccurred
                    && mObserverMap.contains(entry.getKey()))) {
                Event event = downEvent(observer.mState);
                pushParentState(getStateAfter(event));
                 //回调观察者
                observer.dispatchEvent(lifecycleOwner, event);
                popParentState();
            }
        }
    }

    // happens only on the top of stack (never in reentrance),
    // so it doesn't have to take in account parents
    private void sync() {
        LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
        if (lifecycleOwner == null) {
            Log.w(LOG_TAG, "LifecycleOwner is garbage collected, you shouldn't try dispatch "
                    + "new events from it.");
            return;
        }
        while (!isSynced()) {
            mNewEventOccurred = false;
            // no need to check eldest for nullability, because isSynced does it for us.
            if (mState.compareTo(mObserverMap.eldest().getValue().mState)  newest = mObserverMap.newest();
            if (!mNewEventOccurred && newest != null
                    && mState.compareTo(newest.getValue().mState) > 0) {
                //在里面会回调所有观察者
                forwardPass(lifecycleOwner);
            }
        }
        mNewEventOccurred = false;
    }

    static State min(@NonNull State state1, @Nullable State state2) {
        return state2 != null && state2.compareTo(state1) 

通过以上的代码我们可以大概猜出组件生命周期发生改变的适合是如何通知Lifecycle的,如下图: !

那么,我们通过源码来看一看,组件的生命周期变化的时候是如何通知Lifecycle的。

Lifecycle的实现类LifecycleRegistry是在Activity的父类ComponentActivity中被实例化的,所以我们开去ComponentActivity中看看,一下是ComponentActivity相关代码:

@RestrictTo(LIBRARY_GROUP)
public class ComponentActivity extends Activity implements LifecycleOwner {
    private SimpleArrayMap, ExtraData> mExtraDataMap =
            new SimpleArrayMap();

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

    @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);
        //2、
        ReportFragment.injectIfNeededIn(this);
    }

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

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

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

    @RestrictTo(LIBRARY_GROUP)
    public static class ExtraData {
    }
}

在注释1处LifecycleRegistry被创建了,在注释3处,将LifecycleRegistry的State设置为CREATED,注释2处的将ComponentActivity注入到ReportFragment中。正常来说,从以上结构来看,组件的生命周期事件应该是在ComponentActivity中被分发给LifecycleRegistry的,但实际上,并不是,而是由ReportFragment来进行分发的,所以接下来我们要进入ReportFragment中看看:

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class ReportFragment extends Fragment {
    private static final String REPORT_FRAGMENT_TAG = "androidx.lifecycle"
            + ".LifecycleDispatcher.report_fragment_tag";
    public static void injectIfNeededIn(Activity 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();
        }
    }
    static ReportFragment get(Activity activity) {
        return (ReportFragment) activity.getFragmentManager().findFragmentByTag(
                REPORT_FRAGMENT_TAG);
    }
   ...
    @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);
    }

    //...
    private void dispatch(Lifecycle.Event event) {
        Activity activity = getActivity();
         //判断当前组件是否实现了LifecycleRegistryOwner接口
         //LifecycleRegistryOwner是LifecycleOwner的子类
         //两者区别的它们的getLifecycle()方法返回的类型不一样
        if (activity instanceof LifecycleRegistryOwner) {
            ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
            return;
        }
        //判断当前组件是否实现了LifecycleOwner接口
        if (activity instanceof LifecycleOwner) {
            Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
            if (lifecycle instanceof LifecycleRegistry) {
                ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
            }
        }
    }
    //...
}

通过以上代码我们可以看出,在ReportFragment的各个生命周期的方法中,会调用dispatch()方法,而dispatch方法中就会将组件相对应的事件分发给Lifecycle,最终会在LifecycleRegistry进行状态更新以及通知所有的观察者。

以上就是Lifecycle感知组件生命周期,并通知观察者的流程。

Lifecycle中的状态机概念

为什么会存在状态机的概念呢?*因为Lifecycle中的观察者可能会在Activity的任意一个生命周期中添加进来,所以*为了避免后面添加的观察者错失组件前面的生命周期,就用状态机中的状态配合组件的生命周期的事件来记录Activity的生命周期轨迹。大家还记得Lifecycle中的两个枚举类吗,那就是状态机用来记录Activity的生命周期记录轨迹的。我们可以先来看一下这张图(紫色字体为Lifecycle.State枚举类中的常量,黑色字为Lifecycle.EVENT中的常量,对应的是组件的生命周期)**:

Activity等组件的生命周期发生改变的后,就会把相对应的生命周期传到LifecycleLifecycle会将组件的生命周期和State枚举类中的枚举常量对应保存起来并与每个观察者进行绑定,具体对应如上图。在Lifecycle中,为了更好的了解组件的生命周期轨迹,将组件的生命周期分为两个概念,第一个概念叫“生命周期的前进”,指的是onCreate->onStart->onResume这个过程,第二个概念叫 “生命周期的倒退” ,指的是onPause->onStop->onDestroy这个过程(注意,前进和后退不需要三个方法都调用,两个方法调用也可以)。接下来我们聊聊Lifecycle.State的作用:

 public enum State {
        
        DESTROYED,


        INITIALIZED,

 
        CREATED,

    
        STARTED,

    
        RESUMED;

        //比较枚举常量的序数大小的方法
        public boolean isAtLeast(@NonNull State state) {
            return compareTo(state) >= 0;
        }
    }

Lifecycle.State会保存在观察者中去的,其意义是,当我们在onStart方法中注册一个观察者时,为了让这个观察者不错过之前执行过了的onCreate生命周期,在添加观察者的时候会通过一段代码来弥补:

 @Override
    public void addObserver(@NonNull LifecycleObserver observer) {
        //观察者刚注册的时候,初始化的状态INITIALIZED
        State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
        //把观察者和状态封装在一起,这样能更好的绑定
        ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
        ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);

        boolean isReentrance = mAddingObserverCounter != 0 || mHandlingEvent;
        State targetState = calculateTargetState(observer);
        mAddingObserverCounter++;
        //如果观察者的状态和Lifecycle不同步,就先同步,在同步的好就会进行回调观察者
        //如果我们是在onStart中注册的观察者,肯定不同步(这里建议去自行断点测试下)
        while ((statefulObserver.mState.compareTo(targetState) 

在以上代码中会对比观察者和Lifecycle保存的组组件的生命周期是否同步,如果不同步,就先进行同步,同步的时候就会回调观察者。以下是代码测试:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MainActivity.this.getLifecycle().addObserver(new Feel());
    }
}
@SuppressLint("RestrictedApi")
public class Feel implements GenericLifecycleObserver {
    @Override
    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
        Log.e("Feel----------->","Activity:"+event.name()+"------Lifecycle.State:------->"+source.getLifecycle().getCurrentState());
    }
}

一下是输出日志:

2022-08-04 16:01:23.745 26582-26582/com.kuer.ke_livedata E/Feel----------->: Activity:ON_CREATE------Lifecycle.State:------->STARTED
2022-08-04 16:01:23.745 26582-26582/com.kuer.ke_livedata E/Feel----------->: Activity:ON_START------Lifecycle.State:------->STARTED
2022-08-04 16:01:23.745 26582-26582/com.kuer.ke_livedata E/Feel----------->: Activity:ON_RESUME------Lifecycle.State:------->RESUMED

总结:通过以上代码,就可以了解到Lifecycle的状态机的作用了,其实就是用来同步组件和Lifecycle和观察者的生命周期状态的。

【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容