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();
    }

首先调用FragmentManagerImplattachActivity方法,通过其参数来设置FragmentManager中的FragmentActivity类型变量mActivityFragmentContainer类型变量mContainerFragment类型的parent.parentFragmentActivity.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;
    }

其次调用FragmentManagerImpldispatchCreate来设置FragmentManagerImpl内部保持当前Fragment状态的mCurstateFragment.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是否为nullnewState已经初始过。 然后把新的状态值赋值给mCurState变量。并通过mActive判断当前活动的fragment是否为空,不为空的话开始遍历的更新fragment的状态。

onStart

protected void onStart() {
        super.onStart();
        ...
        if (!mCreated) {
            mCreated = true;
            mFragments.dispatchActivityCreated();
        }
        mFragments.noteStateNotSaved();
        mFragments.execPendingActions();
        ...
        mFragments.dispatchStart();
        ...
    }

首先通过dispatchActivityCreated方法调用FragmentManagerImpldispatchActivityCreated方法。

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方法设置mCurStateFragment.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) {
            ...

            ...
        }
    }
    ...
    }

results matching ""

    No results matching ""