美文网首页
Fragment生命周期

Fragment生命周期

作者: gczxbb | 来源:发表于2018-05-07 15:19 被阅读15次

    Fragment依赖于Activity,生命周期与Activity的类似。
    六个状态

    static final int INITIALIZING = 0;     // Not yet created.
    static final int CREATED = 1;          // Created.
    static final int ACTIVITY_CREATED = 2; // activity完成创建
    static final int STOPPED = 3;          // created, not started.
    static final int STARTED = 4;          //  not resumed.
    static final int RESUMED = 5;          // resumed.
    

    FragmentManager#moveToState方法。

    void moveToState(Fragment f, int newState, int transit, int 
                        transitionStyle,boolean keepActive) {
        if (f.mState < newState) {
            switch (f.mState) {//创建与初始化
                case Fragment.INITIALIZING:
                case Fragment.CREATED:
                case Fragment.ACTIVITY_CREATED:
                case Fragment.STOPPED:        
                case Fragment.STARTED:
            }
        } else if (f.mState > newState) {
            switch (f.mState) {//暂停或销毁
                case Fragment.RESUMED:
                case Fragment.STARTED:
                case Fragment.STOPPED:
                case Fragment.ACTIVITY_CREATED:
                case Fragment.CREATED:  
            }
        }
    }
    

    比较Fragment当前mState和新newState状态。

    • 若新newState大于mState,Fragment创建与初始化。
    • 若新newState小于mState,Fragment暂停或销毁。
    • 状态转换过程触发一系列生命周期方法。

    启动生命周期

    Fragment创建启动生命周期。 Fragment创建启动生命周期.jpg
    1,INITIALIZING->CREATED

    Fragment还未初始化。
    Fragment#onAttach方法。

    public void onAttach(Context context) {
        mCalled = true;
        final Activity hostActivity = 
                        mHost == null ? null : mHost.getActivity();
        if (hostActivity != null) {
            mCalled = false;
            onAttach(hostActivity);
        }
    }
    

    mHost是FragmentHostCallback类型,在onAttach执行前赋值。获取依附的Activity,触发Fragment#onAttach方法。
    Fragment#performCreate方法。

    void performCreate(Bundle savedInstanceState) {
        ...
        mState = CREATED;
        mCalled = false;
        onCreate(savedInstanceState);
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                        + " did not call through to super.onCreate()");
        }
    }
    

    触发onCreate,在onCreate会判断恢复savedInstanceState。
    Fragment#onCreate方法。

    public void onCreate(@Nullable Bundle savedInstanceState) {
        mCalled = true;
        restoreChildFragmentState(savedInstanceState);
        if (mChildFragmentManager != null
                    && !mChildFragmentManager.isStateAtLeast(Fragment.CREATED)) {
            mChildFragmentManager.dispatchCreate();
        }
    }
    
    2,CREATED->ACTIVITY_CREATED

    Fragment已经完成了初始化,这一步完成视图创建。

    依次触发:onCreateView,onViewCreated,onActivityCreated

    Fragment#performCreateView方法

    View performCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        ..
        return onCreateView(inflater, container, savedInstanceState);
    }
    

    默认onCreateView返回空,需重写onCreateView方法,创建视图,赋值给f.mView。
    Fragment#onViewCreated方法是空方法,子类中可重写,初始化控件资源。
    Fragment#performActivityCreated方法。

    void performActivityCreated(Bundle savedInstanceState) {
        ..
        mState = ACTIVITY_CREATED;
        mCalled = false;
        onActivityCreated(savedInstanceState);
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                        + " did not call through to super.onActivityCreated()");
        }
        ...
    }
    

    触发onActivityCreated,将mCalled设置为true,子类重写该方法。

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        mCalled = true;
    }
    
    3,ACTIVITY_CREATED->STOPPED

    activity完成创建。

    case Fragment.ACTIVITY_CREATED:
        if (newState > Fragment.ACTIVITY_CREATED) {
            f.mState = Fragment.STOPPED;
        }
    
    4,STOPPED->STARTED

    完全创建完成,还未start。

    case Fragment.STOPPED:
    if (newState > Fragment.STOPPED) {
        f.performStart();
    }
    

    Fragment开始可见。
    Fragment#performStart方法。

    void performStart() {
        ...
        mState = STARTED;
        mCalled = false;
        onStart();
        if (!mCalled) {//抛出异常
        }
        ...
    }
    

    触发onStart,将mCalled设置为true,子类可重写该方法。

    5,STARTED->RESUMED
    case Fragment.STARTED:
    if (newState > Fragment.STARTED) {
        f.performResume();
        f.mSavedFragmentState = null;
        f.mSavedViewState = null;
    }
    

    活动状态,可交互。
    Fragment#performResume方法。

    void performResume() {
        ...
        mState = RESUMED;
        mCalled = false;
        onResume();
        if (!mCalled) {//抛出异常
        }
        ..
    }
    

    触发onResume方法,将mCalled设置为true,子类可重写该方法。

    RESUMED状态

    活动状态。


    销毁生命周期

    Fragment销毁生命周期。 Fragment销毁生命周期.jpg
    1,RESUMED->STARTED
    case Fragment.RESUMED:
        if (newState < Fragment.RESUMED) {
            f.performPause();
        }
    

    使Fragment处于暂停状态,可见,无法交互。
    Fragment#performPause方法。

    void performPause() {
        ...
        mState = STARTED;
        mCalled = false;
        onPause();
        if (!mCalled) {//抛出异常
        }
    }
    

    触发Fragment#onPause,将mCalled设置为true,子类可重写该方法。

    2,STARTED->STOPPED
    case Fragment.STARTED:
        if (newState < Fragment.STARTED) {
            f.performStop();
        }
    

    使Fragment不可见。
    Fragment#performStop方法

    void performStop() {
        ...
        mState = STOPPED;
        mCalled = false;
        onStop();
        if (!mCalled) {//抛出异常
        }
    }
    

    触发Fragment#onStop方法,将mCalled设置为true,子类可重写该方法。

    3,STOPPED->ACTIVITY_CREATED
    case Fragment.STOPPED:
        if (newState < Fragment.STOPPED) {
            f.performReallyStop();
        }
    
    4,,ACTIVITY_CREATED->CREATED

    Fragment#performDestroyView方法。

    void performDestroyView() {
        ...
        mState = CREATED;
        mCalled = false;
        onDestroyView();
        ....
    }
    

    销毁与Fragment有关的视图,但未与Activity解除绑定。
    触发Fragment#onDestroyView方法。

    5,CREATED->INITIALIZING

    Fragment#performDestroy方法。

    void performDestroy() {
        ....
        mState = INITIALIZING;
        mCalled = false;
        onDestroy();
        ....
    }
    

    销毁Fragment,触发onDestory方法。
    Fragment#performDetach方法。
    解除与Activity绑定,触发onDetach方法。

    生命周期方法总结
    onAttach,onCreate,onCreateView,onViewCreated,onActivityCreated,onStart,onResume

    onPause,onStop,onDestoryView,onDestory,onDetach。


    任重而道远

    相关文章

      网友评论

          本文标题:Fragment生命周期

          本文链接:https://www.haomeiwen.com/subject/rbdolftx.html