生命周期图
无处不在的FragmentManager
FragmentActivity通过我们熟悉的FragmentManager管理Fragment的生命周期,在FragmentActivity的每个生命周期中,都可以看到FragmentManager的身影:
public class FragmentActivity
final FragmentController mFragments = FragmentController.createController(new FragmentActivity.HostCallbacks());
protected void onCreate(@Nullable Bundle savedInstanceState) {
this.mFragments.dispatchCreate();
}
protected void onStart() {
this.mFragments.dispatchStart();
}
protected void onResume(){
this.mFragments.dispatchResume()
}
protected void onPause() {
this.mFragments.dispatchPause();
}
protected void onStop() {
this.mFragments.dispatchStop();
}
protected void onDestroy() {
this.mFragments.dispatchDestroy();
}
}
mCurState记录Activity生命周期
mFragments通过各种代理,将具体的执行任务交给FragmentManagerImpl(拉到文章末尾查看代理细节),并将Activity的生命周期以数字的形式保存在mCurState属性中,方法实现如下:
final class FragmentManagerImpl{
int mCurState = 0;
void moveToState(int newState, boolean always) {
this.mCurState = newState;
}
private void dispatchStateChange(int nextState) {
this.moveToState(nextState, false);
}
public void dispatchCreate() {
this.dispatchStateChange(1);
}
public void dispatchActivityCreated() {
this.dispatchStateChange(2);
}
public void dispatchStart() {
this.dispatchStateChange(3);
}
public void dispatchResume() {
this.dispatchStateChange(4);
}
public void dispatchPause() {
this.dispatchStateChange(3);
}
public void dispatchStop() {
this.dispatchStateChange(2);
}
public void dispatchDestroyView() {
this.dispatchStateChange(1);
}
public void dispatchDestroy() {
this.dispatchStateChange(0);
}
}
mState记录Fragment生命周期
在Fragment中也存在一个记录生命周期的int属性mState:
void performCreate(Bundle savedInstanceState) {
this.mState = 1;
this.onCreate(savedInstanceState);
}
void performActivityCreated(Bundle savedInstanceState) {
this.mState = 2;
this.onActivityCreated(savedInstanceState);
}
void performStart() {
this.mState = 3;
this.onStart();
}
void performResume() {
this.mState = 4;
this.onResume();
}
void performPause() {
this.mState = 3;
this.onPause();
}
void performStop() {
this.mState = 2;
this.onStop();
}
void performDestroyView() {
this.mState = 1;
this.onDestroyView();
}
void performDestroy() {
this.mState = 0;
this.onDestroy();
}
当我们调用了FragmentTransaction的commit方法之后,FragmentManagerImpl内部通过对比mCurState和mState依次调用Fragment的生命周期函数:
利用mCurState、mState同步生命周期
我们已经知道了Activity使用mCurState记录生命周期,Fragment使用mState记录生命周期,下表是两个属性在生命周期的赋值表:
生命周期 | mCurState | mState |
---|---|---|
onCreate | 1 | 1 |
onActivityCreated | 2 | 2 |
onStart | 3 | 3 |
onResume | 4 | 4 |
onPause | 3 | 3 |
onStop | 2 | 2 |
onDestroyView | 1 | 1 |
onDestroy | 0 | 0 |
可以看到Activity和Fragment中,生命周期的值是保持一致的。
FragmentManagerImpl对比mCurState和mState,控制Fragment的生命周期:
final class FragmentManagerImpl {
void moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive) {
//比较mState,mCurState
if (f.mState <= newState) {
label297:
switch (f.mState) {
case 0:
if (newState > 0) {
//调用onAttach
f.onAttach(this.mHost.getContext());
if (!f.mIsCreated) {
//调用onCreate
f.performCreate(f.mSavedFragmentState);
}
}
case 1:
if (newState > 1) {
//调用onCreateView
f.performCreateView(f.performGetLayoutInflater(f.mSavedFragmentState), container, f.mSavedFragmentState);
if (f.mView == null) {
} else {
//调用onViewCreated
f.onViewCreated(f.mView, f.mSavedFragmentState);
}
//调用onActivityCreated
f.performActivityCreated(f.mSavedFragmentState);
}
case 2:
if (newState > 2) {
//调用onStart
f.performStart();
}
case 3:
break;
default:
//循环,进入下一个生命周期的处理
break label297;
}
} else if (f.mState > newState) {
switch (f.mState) {
case 4:
if (newState < 4) {
//调用onPause
f.performPause();
}
case 3:
if (newState < 3) {
//调用onStop
f.performStop();
}
case 2:
if (newState < 2) {
if (f.mView != null && this.mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
this.saveFragmentViewState(f);
}
//调用onDestroyView
f.performDestroyView();
}
case 1:
if (newState < 1) {
if (f.getAnimatingAway() == null && f.getAnimator() == null) {
if (!f.mRetaining) {
//调用onDestroy
f.performDestroy();
this.dispatchOnFragmentDestroyed(f, false);
} else {
f.mState = 0;
}
//onDetach
f.performDetach();
} else {
f.setStateAfterAnimating(newState);
newState = 1;
}
}
}
}
}
}
这个方法中,不止同步了Activity的生命周期,还做了切换动画、保存数据、回复数据,感兴趣的同学可以去看看源码。