FragmentActivity
主要完成FragmentManager配置
、保存fragment
、维护fragment声明周期
,保持fragment声明周期与Activity相对应、和设置Fragment.startActivityForResult
方法。
fragmentManager初始化
通过内部FragmentManagerImpl
类型常量mFragments
实例化FragmentManager
...
final FragmentManager mFragments = new FragmentManagerImpl();
...
通过getSupportFragmentManager
获取FragmentManager
public FragmentManager getSupportFragmentManager(){
return mFragments;
}
保存状态
/**
* Save all appropriate fragment state.
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
}
通过调用FragmentManagerImpl
类型的常量mFragments
的.saveAllState
方法获取实现Parcelable
接口的FragmentManagerState
.并将其放入Bundle
类型的outState
中保存起来。在当前Activity非用户手动退出重新启动时使用。
生命周期
onCreate
/**
* Perform initialization of all fragments and loaders.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
mFragments.attachActivity(this, mContainer, null);
// Old versions of the platform didn't do this!
if (getLayoutInflater().getFactory() == null) {
getLayoutInflater().setFactory(this);
}
super.onCreate(savedInstanceState);
NonConfigurationInstances nc = (NonConfigurationInstances)
getLastNonConfigurationInstance();
if (nc != null) {
mAllLoaderManagers = nc.loaders;
}
if (savedInstanceState != null) {
Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
}
mFragments.dispatchCreate();
}
首先调用FragmentManagerImpl
的attachActivity
方法,通过其参数来设置FragmentManager
中的FragmentActivity
类型变量mActivity
、FragmentContainer
类型变量mContainer
、Fragment
类型的parent
.parent
在FragmentActivity.onCreate
中设置为null
.
public void attachActivity(FragmentActivity activity,
FragmentContainer container, Fragment parent) {
if (mActivity != null) throw new IllegalStateException("Already attached");
mActivity = activity;
mContainer = container;
mParent = parent;
}
其次调用FragmentManagerImpl
的dispatchCreate
来设置FragmentManagerImpl
内部保持当前Fragment
状态的mCurstate
为Fragment.CREATED
public void dispatchCreate() {
mStateSaved = false;
moveToState(Fragment.CREATED, false);
}
在dispatchCreate()
方法中会调用内部函数moveToState(int,int,boolean)
,在moveToState(int,int,boolean)
内部自动调用moveToState(int,int,int,boolean)
方法:
void moveToState(int newState, int transit, int transitStyle, boolean always) {
if (mActivity == null && newState != Fragment.INITIALIZING) {
throw new IllegalStateException("No activity");
}
if (!always && mCurState == newState) {
return;
}
mCurState = newState;
if (mActive != null) {
boolean loadersRunning = false;
for (int i=0; i<mActive.size(); i++) {
Fragment f = mActive.get(i);
if (f != null) {
moveToState(f, newState, transit, transitStyle, false);
if (f.mLoaderManager != null) {
loadersRunning |= f.mLoaderManager.hasRunningLoaders();
}
}
}
if (!loadersRunning) {
startPendingDeferredFragments();
}
if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) {
mActivity.supportInvalidateOptionsMenu();
mNeedMenuInvalidate = false;
}
}
}
在moveToState(int,int,int,boolean)
方法内,首先判断当前activity
是否为null
及newState
已经初始过。
然后把新的状态值赋值给mCurState
变量。并通过mActive
判断当前活动的fragment
是否为空,不为空的话开始遍历的更新fragment
的状态。
onStart
protected void onStart() {
super.onStart();
...
if (!mCreated) {
mCreated = true;
mFragments.dispatchActivityCreated();
}
mFragments.noteStateNotSaved();
mFragments.execPendingActions();
...
mFragments.dispatchStart();
...
}
首先通过dispatchActivityCreated
方法调用FragmentManagerImpl
的dispatchActivityCreated
方法。
public void dispatchActivityCreated() {
mStateSaved = false;
moveToState(Fragment.ACTIVITY_CREATED, false);
}
这个方法会调用moveToState(int,boolean)
方法,并设置代表Fragment状态
的mCurState
变量为Fragment.ACTIVITY_CREATED
。
接着调用execPendingActions
查看是否有fragment操作需要执行。
public boolean execPendingActions() {
...
while(true){
...
synchronized (this) {
if (mPendingActions == null || mPendingActions.size() == 0) {
break;
}
...
}
...
}
...
}
最后调用dispatchStart
方法设置mCurState
为Fragment.STARTED
onCreateView
onResume
onPause
onStop
onDestroy
备用代码
//此处第一次调用该方法f.state为INITIALIZING,newState为mCurState为Fragment.ACTIVITY_CREATED
void moveToState(Fragment f, int newState, int transit, int transitionStyle,
boolean keepActive) {
//设置newState为Fragment.Create
if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
newState = Fragment.CREATED;
}
...
//如果当前fragment状态在onResume之前
if(f.mState < newState){
...
switch (f.mState) {
...
...
}
}
...
}