如何建设一个电商网站,一个空间如何做多个网站,医疗网站建站需求,个人接外贸订单网站我们知道Fragment的生命周期依附于相应Activity的生命周期#xff0c;如果activity A调用了onPause#xff0c;则A里面的fragment也会相应收到onPause回调#xff0c;这里以support27.1.1版本的源码来说明Fragment生命周期onPause的事情。 当activity执行onPause时#xff… 我们知道Fragment的生命周期依附于相应Activity的生命周期如果activity A调用了onPause则A里面的fragment也会相应收到onPause回调这里以support27.1.1版本的源码来说明Fragment生命周期onPause的事情。 当activity执行onPause时进入如下的代码 /*** Dispatch onPause() to fragments.*/Overrideprotected void onPause() {super.onPause();mResumed false;if (mHandler.hasMessages(MSG_RESUME_PENDING)) {mHandler.removeMessages(MSG_RESUME_PENDING);onResumeFragments();}mFragments.dispatchPause();//关注这里} mFragments.dispatchPause()这里进入到Fragment的生命周期分发流程中mFragments其实是一个FragmentController对象dispatchPause相应代码如下 /*** Moves all Fragments managed by the controllers FragmentManager* into the pause state.* pCall when Fragments should be paused.** see Fragment#onPause()*/public void dispatchPause() {mHost.mFragmentManager.dispatchPause();} 这里也是简单的调用转发进入到FragmentManager里面的dispatchPause方法如下
public void dispatchCreate() {mStateSaved false;mStopped false;dispatchStateChange(Fragment.CREATED);}public void dispatchActivityCreated() {mStateSaved false;mStopped false;dispatchStateChange(Fragment.ACTIVITY_CREATED);}public void dispatchStart() {mStateSaved false;mStopped false;dispatchStateChange(Fragment.STARTED);}public void dispatchResume() {mStateSaved false;mStopped false;dispatchStateChange(Fragment.RESUMED);}public void dispatchPause() {dispatchStateChange(Fragment.STARTED);}public void dispatchStop() {mStopped true;dispatchStateChange(Fragment.STOPPED);}public void dispatchReallyStop() {dispatchStateChange(Fragment.ACTIVITY_CREATED);}public void dispatchDestroyView() {dispatchStateChange(Fragment.CREATED);}public void dispatchDestroy() {mDestroyed true;execPendingActions();dispatchStateChange(Fragment.INITIALIZING);mHost null;mContainer null;mParent null;}private void dispatchStateChange(int nextState) {try {mExecutingActions true;moveToState(nextState, false);} finally {mExecutingActions false;}execPendingActions();} 这里有一堆的生命周期事件分发入口说明Activity的相应生命周期事件都会分发到这里对应的方法当前的onPause会进入dispatchPause方法然后调用dispatchStateChange方法注意调用参数为Fragment.STARTED值为4这里看看Fragment的几个状态定义如下 static final int INITIALIZING 0; // Not yet created.static final int CREATED 1; // Created.static final int ACTIVITY_CREATED 2; // The activity has finished its creation.static final int STOPPED 3; // Fully created, not started.static final int STARTED 4; // Created and started, not resumed.static final int RESUMED 5; // Created started and resumed.int mState INITIALIZING; 接着进入moveToState方法重点来了即将进入Fragment的状态转换了传递给moveToState的参数nextState为Fragment.STARTED值为4always为false相应代码如下 /*** Changes the state of the fragment manager to {code newState}. If the fragment manager* changes state or {code always} is {code true}, any fragments within it have their* states updated as well.** param newState The new state for the fragment manager* param always If {code true}, all fragments update their state, even* if {code newState} matches the current fragment managers state.*/void moveToState(int newState, boolean always) {if (mHost null newState ! Fragment.INITIALIZING) {throw new IllegalStateException(No activity);}if (!always newState mCurState) {return;}mCurState newState;//1关注这里if (mActive ! null) {// Must add them in the proper order. mActive fragments may be out of orderfinal int numAdded mAdded.size();for (int i 0; i numAdded; i) {Fragment f mAdded.get(i);moveFragmentToExpectedState(f);//2关注这里}// Now iterate through all active fragments. These will include those that are removed// and detached.final int numActive mActive.size();for (int i 0; i numActive; i) {Fragment f mActive.valueAt(i);if (f ! null (f.mRemoving || f.mDetached) !f.mIsNewlyAdded) {moveFragmentToExpectedState(f);}}startPendingDeferredFragments();if (mNeedMenuInvalidate mHost ! null mCurState Fragment.RESUMED) {mHost.onSupportInvalidateOptionsMenu();mNeedMenuInvalidate false;}}}moveToState中关注两点首先是将状态newState保存到mCurState为STARTED4中其次遍历集合mAdded并调用moveFragmentToExpectedState改变相应fragment的生命周期状态。 mAdded保存当前activity中已经添加的fragment实例。 moveFragmentToExpectedState代码如下 /*** Moves a fragment to its expected final state or the fragment managers state, depending* on whether the fragment managers state is raised properly.** param f The fragment to change.*/void moveFragmentToExpectedState(Fragment f) {if (f null) {return;}int nextState mCurState;if (f.mRemoving) {if (f.isInBackStack()) {nextState Math.min(nextState, Fragment.CREATED);} else {nextState Math.min(nextState, Fragment.INITIALIZING);}}//状态转换moveToState(f, nextState, f.getNextTransition(), f.getNextTransitionStyle(), false);if (f.mView ! null) {// Move the view if it is out of orderFragment underFragment findFragmentUnder(f);if (underFragment ! null) {final View underView underFragment.mView;// make sure this fragment is in the right order.final ViewGroup container f.mContainer;int underIndex container.indexOfChild(underView);int viewIndex container.indexOfChild(f.mView);if (viewIndex underIndex) {container.removeViewAt(viewIndex);container.addView(f.mView, underIndex);}}if (f.mIsNewlyAdded f.mContainer ! null) {// Make it visible and run the animationsif (f.mPostponedAlpha 0f) {f.mView.setAlpha(f.mPostponedAlpha);}f.mPostponedAlpha 0f;f.mIsNewlyAdded false;// run animations:AnimationOrAnimator anim loadAnimation(f, f.getNextTransition(), true,f.getNextTransitionStyle());if (anim ! null) {setHWLayerAnimListenerIfAlpha(f.mView, anim);if (anim.animation ! null) {f.mView.startAnimation(anim.animation);} else {anim.animator.setTarget(f.mView);anim.animator.start();}}}}if (f.mHiddenChanged) {completeShowHideFragment(f);}}该方法主要调用moveToState执行Fragment状态转换这里将执行真正的生命周期方法代码如下 SuppressWarnings(ReferenceEquality)void moveToState(Fragment f, int newState, int transit, int transitionStyle,boolean keepActive) {// Fragments that are not currently added will sit in the onCreate() state.if ((!f.mAdded || f.mDetached) newState Fragment.CREATED) {newState Fragment.CREATED;}if (f.mRemoving newState f.mState) {if (f.mState Fragment.INITIALIZING f.isInBackStack()) {// Allow the fragment to be created so that it can be saved later.newState Fragment.CREATED;} else {// While removing a fragment, we cant change it to a higher state.newState f.mState;}}// Defer start if requested; dont allow it to move to STARTED or higher// if its not already started.if (f.mDeferStart f.mState Fragment.STARTED newState Fragment.STOPPED) {newState Fragment.STOPPED;}if (f.mState newState) {// For fragments that are created from a layout, when restoring from// state we dont want to allow them to be created until they are// being reloaded from the layout.//该分支表示生命周期转换 create - start - resume} else if (f.mState newState) {//该分支表示生命周期转换 pause - stop - destoryView - destory - detachswitch (f.mState) {case Fragment.RESUMED:if (newState Fragment.RESUMED) {if (DEBUG) Log.v(TAG, movefrom RESUMED: f);f.performPause();dispatchOnFragmentPaused(f, false);}// fall throughcase Fragment.STARTED:if (newState Fragment.STARTED) {if (DEBUG) Log.v(TAG, movefrom STARTED: f);f.performStop();dispatchOnFragmentStopped(f, false);}// fall throughcase Fragment.STOPPED:if (newState Fragment.STOPPED) {if (DEBUG) Log.v(TAG, movefrom STOPPED: f);f.performReallyStop();}// fall throughcase Fragment.ACTIVITY_CREATED:if (newState Fragment.ACTIVITY_CREATED) {if (DEBUG) Log.v(TAG, movefrom ACTIVITY_CREATED: f);if (f.mView ! null) {// Need to save the current view state if not// done already.if (mHost.onShouldSaveFragmentState(f) f.mSavedViewState null) {saveFragmentViewState(f);}}f.performDestroyView();dispatchOnFragmentViewDestroyed(f, false);if (f.mView ! null f.mContainer ! null) {// Stop any current animations:f.mContainer.endViewTransition(f.mView);f.mView.clearAnimation();AnimationOrAnimator anim null;if (mCurState Fragment.INITIALIZING !mDestroyed f.mView.getVisibility() View.VISIBLE f.mPostponedAlpha 0) {anim loadAnimation(f, transit, false,transitionStyle);}f.mPostponedAlpha 0;if (anim ! null) {animateRemoveFragment(f, anim, newState);}f.mContainer.removeView(f.mView);}f.mContainer null;f.mView null;f.mInnerView null;f.mInLayout false;}// fall throughcase Fragment.CREATED:if (newState Fragment.CREATED) {if (mDestroyed) {// The fragments containing activity is// being destroyed, but this fragment is// currently animating away. Stop the// animation right now -- it is not needed,// and we cant wait any more on destroying// the fragment.if (f.getAnimatingAway() ! null) {View v f.getAnimatingAway();f.setAnimatingAway(null);v.clearAnimation();} else if (f.getAnimator() ! null) {Animator animator f.getAnimator();f.setAnimator(null);animator.cancel();}}if (f.getAnimatingAway() ! null || f.getAnimator() ! null) {// We are waiting for the fragments view to finish// animating away. Just make a note of the state// the fragment now should move to once the animation// is done.f.setStateAfterAnimating(newState);newState Fragment.CREATED;} else {if (DEBUG) Log.v(TAG, movefrom CREATED: f);if (!f.mRetaining) {f.performDestroy();dispatchOnFragmentDestroyed(f, false);} else {f.mState Fragment.INITIALIZING;}f.performDetach();dispatchOnFragmentDetached(f, false);if (!keepActive) {if (!f.mRetaining) {makeInactive(f);} else {f.mHost null;f.mParentFragment null;f.mFragmentManager null;}}}}}}if (f.mState ! newState) {Log.w(TAG, moveToState: Fragment state for f not updated inline; expected state newState found f.mState);f.mState newState;}}moveToState包含两大分支逻辑分别是创建与销毁由于我们这里分析的是onPause所以创建的分支省略了这里newState值为Fragment.STARTEDf.mSate为Fragment.RESUMED因此进入到Fragment.RESUMED对应的销毁分支即执行如下代码 case Fragment.RESUMED:if (newState Fragment.RESUMED) {if (DEBUG) Log.v(TAG, movefrom RESUMED: f);f.performPause();dispatchOnFragmentPaused(f, false);} 由于newState为Fragment.STARTED其值为4小于Fragment.RESUMED(5)因此调用f.performPause代码如下
void performPause() {mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);if (mChildFragmentManager ! null) {mChildFragmentManager.dispatchPause();}mState STARTED;mCalled false;onPause();if (!mCalled) {throw new SuperNotCalledException(Fragment this did not call through to super.onPause());}} 这里首先处理了生命周期变化handleLifecycleEvent然后如果当前fragment还有子fragment的话子fragment的生命周期会通过mChildFragmentManager.dispatchPause()来分发这里还将当前fragment的mState变量赋值为STARTED(4)最后调用生命周期方法onPause到这里算是走完了onPause的生命周期流程为了逻辑的完整性我们继续分析。 上一步继续调用dispatchOnFragmentPaused方法代码如下
void dispatchOnFragmentPaused(Fragment f, boolean onlyRecursive) {if (mParent ! null) {FragmentManager parentManager mParent.getFragmentManager();if (parentManager instanceof FragmentManagerImpl) {((FragmentManagerImpl) parentManager).dispatchOnFragmentPaused(f, true);}}for (PairFragmentLifecycleCallbacks, Boolean p : mLifecycleCallbacks) {if (!onlyRecursive || p.second) {p.first.onFragmentPaused(this, f);}}} 首先判断有没有父fragment有的话同样进行生命周期分发其次遍历mLifecycleCallbacks并进行生命周期分发这里的mLifecycleCallbacks其实是我们通过activity的FragmentManager调用registerFragmentLifecycleCallbacks方法注册的生命周期回调函数。 不难看出onPause对应的Fragment.RESUMED分支其实只是处理生命周期相关的回调而已。 onPause分支总结 1 处理mLifecycleRegistry中的回调。 2 通过mChildFragmentManager.dispatchPause()分发其子fragment的生命周期回调。 3 调用onPause执行自己的生命周期方法同时mState STARTED。 4 如果有父framnet则分发父frament的生命周期回调。 5 执行保存在mLifecycleCallbacks中的生命周期回调比如LeakCanary的内存泄漏判断就是此时触发的。 onPause执行完成注意到moveToState中的分支都不带break因此会继续执行到下一分支代码如下 case Fragment.RESUMED:if (newState Fragment.RESUMED) {if (DEBUG) Log.v(TAG, movefrom RESUMED: f);f.performPause();dispatchOnFragmentPaused(f, false);}// fall throughcase Fragment.STARTED://执行到这里if (newState Fragment.STARTED) {if (DEBUG) Log.v(TAG, movefrom STARTED: f);f.performStop();dispatchOnFragmentStopped(f, false);}// fall throughcase Fragment.STOPPED:if (newState Fragment.STOPPED) {if (DEBUG) Log.v(TAG, movefrom STOPPED: f);f.performReallyStop();} 穿透到Fragment.STARTED这里但是注意这里的代码执行时有条件的此时newStateFragment.STARTED条件不满足因此不会继续执行分支的代码之后的分支也不满足条件因此最后跳出整个大的switch块。 自此Fragment的onPause生命周期切换完成。 执行完onPause之后Fragment的生命周期状态mState为Fragment.STARTED。