我的网站突然打不开了国内网络推广渠道
我们知道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 controller's FragmentManager* into the pause state.* <p>Call 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(值为4),always为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 manager's 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(为STARTED,4)中,其次遍历集合mAdded并调用moveFragmentToExpectedState改变相应fragment的生命周期状态。
mAdded:保存当前activity中已经添加的fragment实例。
moveFragmentToExpectedState代码如下:
/*** Moves a fragment to its expected final state or the fragment manager's state, depending* on whether the fragment manager's 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 can't change it to a higher state.newState = f.mState;}}// Defer start if requested; don't allow it to move to STARTED or higher// if it's 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 don't 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 fragment's containing activity is// being destroyed, but this fragment is// currently animating away. Stop the// animation right now -- it is not needed,// and we can't 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 fragment's 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.STARTED,f.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 (Pair<FragmentLifecycleCallbacks, 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这里,但是注意这里的代码执行时有条件的,此时newState=Fragment.STARTED,条件不满足因此不会继续执行分支的代码,之后的分支也不满足条件,因此最后跳出整个大的switch块。
自此Fragment的onPause生命周期切换完成。
执行完onPause之后,Fragment的生命周期状态mState为Fragment.STARTED。