@OverrideprotectedvoidonCreate(@NullableBundle savedInstanceState){// Restore the Saved State first so that it is available to// OnContextAvailableListener instances
mSavedStateRegistryController.performRestore(savedInstanceState);
mContextAwareHelper.dispatchOnContextAvailable(this);super.onCreate(savedInstanceState);
mActivityResultRegistry.onRestoreInstanceState(savedInstanceState);ReportFragment.injectIfNeededIn(this);// 添加一个透明fragmentif(mContentLayoutId !=0){setContentView(mContentLayoutId);}}publicstaticvoidinjectIfNeededIn(Activity activity){if(Build.VERSION.SDK_INT >=29){// On API 29+, we can register for the correct Lifecycle callbacks directlyLifecycleCallbacks.registerIn(activity);}// Prior to API 29 and to maintain compatibility with older versions of// ProcessLifecycleOwner (which may not be updated when lifecycle-runtime is updated and// need to support activities that don't extend from FragmentActivity from support lib),// use a framework fragment to get the correct timing of Lifecycle events// 这里根据29版本进行了区分,为了兼容高版本// 给Activity添加fragment用于监听生命周期事件(glide中有同样用法)//上面的英文注释也解释了reportfragment的作用android.app.FragmentManager manager = activity.getFragmentManager();if(manager.findFragmentByTag(REPORT_FRAGMENT_TAG)==null){
manager.beginTransaction().add(newReportFragment(), REPORT_FRAGMENT_TAG).commit();// Hopefully, we are the first to make a transaction.
manager.executePendingTransactions();}}
privatevoiddispatch(@NonNullLifecycle.Event event){if(Build.VERSION.SDK_INT <29){// Only dispatch events from ReportFragment on API levels prior// to API 29. On API 29+, this is handled by the ActivityLifecycleCallbacks// added in ReportFragment.injectIfNeededIndispatch(getActivity(), event);}}staticvoiddispatch(@NonNullActivity activity,@NonNullLifecycle.Event event){if(activity instanceofLifecycleRegistryOwner){((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);return;}if(activity instanceofLifecycleOwner){Lifecycle lifecycle =((LifecycleOwner) activity).getLifecycle();if(lifecycle instanceofLifecycleRegistry){((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);}}}publicvoidhandleLifecycleEvent(@NonNullLifecycle.Event event){enforceMainThreadIfNeeded("handleLifecycleEvent");moveToState(event.getTargetState());}
处理事件,根据事件获得状态
publicvoidhandleLifecycleEvent(@NonNullLifecycle.Event event){enforceMainThreadIfNeeded("handleLifecycleEvent");moveToState(event.getTargetState());}privatevoidmoveToState(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;}// 和被观察的状态进行同步,前进或者后退状态privatevoidsync(){LifecycleOwner lifecycleOwner = mLifecycleOwner.get();if(lifecycleOwner ==null){thrownewIllegalStateException("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;}