美文网首页
从源码和注释看fragment的生命周期

从源码和注释看fragment的生命周期

作者: 刘孙猫咪 | 来源:发表于2017-07-02 09:53 被阅读0次

    Android 3.0中引入fragments 的概念,其目的是为了解决不同屏幕分辩率的动态和灵活UI设计,它是通过将Activity 的布局分散到frament 中,依托于activity而存在,frament 的生命周期会直接受到activity生命周期的影响,谷歌官网的这张生命周期关系图就说明这些;

    20140719225005356.png

    同时frament 还多了些Activity 没有的生命周期,下面看下frament 的生命周期是如何调用的;

    加载该frament 的时候:

    QQ截图20170702091244.jpg

    切换frament 的时候:

    QQ截图20170702083804.jpg

    这里采用的是add和replace的方式添加fragment和切换的,在加载的时候首先调用的是onAttach();

    onAttach()

        /**
         * Called when a fragment is first attached to its context.
         * {@link #onCreate(Bundle)} will be called after this.
         */
        @CallSuper
        public void onAttach(Context context) {
            mCalled = true;
            final Activity hostActivity = mHost == null ? null : mHost.getActivity();
            if (hostActivity != null) {
                mCalled = false;
                onAttach(hostActivity);
            }
        }
    

    onAttach源码和注释中看到当fragment 第一次被依附于它的上下文的时候会被调用,同时onCreate会在它的后面调用,同时它是通过FragmentHostCallBack中的

    Activity getActivity() {
            return mActivity;
        }
    

    获取到该activity,如果该activity不是空的,就绑定在该activity上面,

    onCreate()

      /**
         * Called to do initial creation of a fragment.  This is called after
         * {@link #onAttach(Activity)} and before
         * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
         */
        @CallSuper
        public void onCreate(@Nullable Bundle savedInstanceState) {
            mCalled = true;
            restoreChildFragmentState(savedInstanceState);
            if (mChildFragmentManager != null
                    && !mChildFragmentManager.isStateAtLeast(Fragment.CREATED)) {
                mChildFragmentManager.dispatchCreate();
            }
        }
    
    

    从注释中看到onCreate方法是调用来创建片段的初始创建,在onAttach之后onCreateView之前调用,

    onCreateView()

       /**
         * Called to have the fragment instantiate its user interface view.    
         */
        @Nullable
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                @Nullable Bundle savedInstanceState) {
            return null;
        }
    

    fragment实例化自己的视图的时候被调用

    onActivityCreate()

      /**
         * Called when the fragment's activity has been created and this
         * fragment's view hierarchy instantiated
         */
        @CallSuper
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            mCalled = true;
        }
    

    fragment的activity已经被创建同时fragment的视图已经初始化也就是fragment的onCreateView被调用后就会调用onActivityCreate生命周期

    onStart()

        /**
         * Called when the Fragment is visible to the user.  This is generally
         * tied to {@link Activity#onStart() Activity.onStart} of the containing
         * Activity's lifecycle.
         */
        @CallSuper
        public void onStart() {
           
          
        }
    

    fragment用户可见的时候调用,通常与activity的onStart联系在一起;

    onResume()

      /**
         * Called when the fragment is visible to the user and actively running.
         * This is generally
         * tied to {@link Activity#onResume() Activity.onResume} of the containing
         * Activity's lifecycle.
         */
        @CallSuper
        public void onResume() {
            mCalled = true;
        }
    

    fragment可见于用户,并且已经运行的时候调用,那就肯定是在onStart之后调用了;
    从运行的效果图看,以上这些就是fragment加载时会调用的生命周期,
    onAttach--->onCreate--->onCreateView--->onActivityCreate--->onStart--->onResume

    再来看看fragment的切换时的生命周期,运行的结果为:
    onPause--->onStop--->onDestoryView--->onDestory--->onDetach

    onPause()

      /**
         * Called when the Fragment is no longer resumed.  This is generally
         * tied to {@link Activity#onPause() Activity.onPause} of the containing
         * Activity's lifecycle.
         */
        @CallSuper
        public void onPause() {
            mCalled = true;
        }
    

    fragment已不resumed了,也就是说用户不可见,并且退出运行的时候会调用;

    onStop()

        /**
         * Called when the Fragment is no longer started.  This is generally
         * tied to {@link Activity#onStop() Activity.onStop} of the containing
         * Activity's lifecycle.
         */
        @CallSuper
        public void onStop() {
            mCalled = true;
        }
    

    fragment已不started就会调用该生命周期

    onDestoryView()

      /**
         * Called when the view previously created by {@link #onCreateView} has
         * been detached from the fragment.  The next time the fragment needs
         * to be displayed, a new view will be created.  This is called
         * after {@link #onStop()} and before {@link #onDestroy()}.  
         */
        @CallSuper
        public void onDestroyView() {
            mCalled = true;
        }
    

    由onCreateView创建的视图已经从fragment脱离的时候调用,同时下次该fragment显示的时会创建一个新的view,该生命周期在onStop之后onDestory之前调用

    onDestory()

        /**
         * Called when the fragment is no longer in use.  This is called
         * after {@link #onStop()} and before {@link #onDetach()}.
         */
        @CallSuper
        public void onDestroy() {
           
        }
    

    fragment不再使用的时候调用,生命周期位于onStop之前onDetach之前

    onDetach()

        /**
         * Called when the fragment is no longer attached to its activity.  This
         * is called after {@link #onDestroy()}.
         */
        @CallSuper
        public void onDetach() {
            mCalled = true;
        }
    

    fragment已经不在依托于activity会调用,生命周期位于onDestory之后;
    如上面写的有不对的欢迎交流。

    相关文章

      网友评论

          本文标题:从源码和注释看fragment的生命周期

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