Lifecycles原理

Lifecycles

作用:管理 Activity 和 Fragment 生命周期
既然可以感知activity和fragment的生命周期,那么必然在其生命周期的方法里添加了一个回调,然后再通知外部

Activity的生命周期方法监听

我们知道Lifecycle 可以监听Activity和fragment生命周期方法,那么是如何监听的呢?
在ComponentActivity类里面,我们并没有看到它在activity的生命周期方法里面直接回调出去
我们知道通过调用registerActivityLifecycleCallbacks方法,也可以获取activity生命周期方法并回调出去。
但是lifecycle不是这么做的,而是注入一个fragment,这样间接的也可以获取activity生命周期,这样的做法和glide是一样的

public class ComponentActivity extends Activity
    implements LifecycleOwner, KeyEventDispatcher.Component {
   ...

    @Override
    @SuppressWarnings("RestrictedApi")
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注入一个fragment
        ReportFragment.injectIfNeededIn(this);
    }
}




public class ReportFragment extends Fragment {
    private static final String REPORT_FRAGMENT_TAG = "androidx.lifecycle"
            + ".LifecycleDispatcher.report_fragment_tag";
    
    //注入一个fragment
    public static void injectIfNeededIn(Activity activity) {
        // ProcessLifecycleOwner should always correctly work and some activities may not extend
        // FragmentActivity from support lib, so we use framework fragments for activities
        android.app.FragmentManager manager = activity.getFragmentManager();
        if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
            manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit();
            // Hopefully, we are the first to make a transaction.
            manager.executePendingTransactions();
        }
    }

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


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //分发生命周期方法
        dispatch(Lifecycle.Event.ON_CREATE);
    }

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

        ...
    private void dispatch(Lifecycle.Event event) {
        Activity activity = getActivity();
        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);
            }
        }
    }
}

Fragment的生命周期方法监听

Fragment则简单的多,直接在Fragment里面的生命周期方法里面回调的

public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner,
        ViewModelStoreOwner{

    
      void performCreate(Bundle savedInstanceState) {
        ...
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
    }

    void performStart() {
        ...
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
        
    }
}

你可能感兴趣的:(Lifecycles原理)