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

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之后;
如上面写的有不对的欢迎交流。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 为什么写这个 在网上也有很多这个例子,但是感觉讲的都不很清楚,于是想自己跑一遍来看看整个过程,话不多说,下面就直接...
    sakurajiang阅读 10,242评论 0 32
  • Fragment 描述:   翻译可以译为:碎片、片段,Android 3.0开始引入fragments 的概念;...
    Lost_Robot阅读 5,774评论 0 11
  • Fragment是什么 说了半天的Fragment,也看到这么多次Fragment这个名词出现,那么Fragmen...
    October5阅读 12,586评论 0 8
  • Activity生命周期 典型生命周期 指在有用户参与的情况下,Activity经历的生命周期的改变。正常情况下,...
    wangsye阅读 5,239评论 0 0
  • 有什么能够阻碍, 我对它的向住。 如天堂般的 致命诱惑, 如此这般的 生活: 有趣有志 有爱有自由。
    雪莉诗话阅读 1,436评论 8 15