fragment源码解析

首先必须了解fragment的生命周期,如下图

 

理解周期的话可以跟activity的生命周期进行比较,注意看注释,认真看源码上的注释,一步一步看。

onAttach()

/**
     * Called when a fragment is first attached to its activity.
     * {@link #onCreate(Bundle)} will be called after this.
     */
    public void onAttach(Activity activity) {
        mCalled = true;
    }

 

onCreateView()

 1 /**
 2      * Called to have the fragment instantiate its user interface view.
 3      * This is optional, and non-graphical fragments can return null (which
 4      * is the default implementation).  This will be called between
 5      * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
 6      * 
 7      * <p>If you return a View from here, you will later be called in
 8      * {@link #onDestroyView} when the view is being released.
 9      * 
10      * @param inflater The LayoutInflater object that can be used to inflate
11      * any views in the fragment,
12      * @param container If non-null, this is the parent view that the fragment's
13      * UI should be attached to.  The fragment should not add the view itself,
14      * but this can be used to generate the LayoutParams of the view.
15      * @param savedInstanceState If non-null, this fragment is being re-constructed
16      * from a previous saved state as given here.
17      * 
18      * @return Return the View for the fragment's UI, or null.
19      */
20     public View onCreateView(LayoutInflater inflater, ViewGroup container,
21             Bundle savedInstanceState) {
22         return null;
23     }

 

onActivityCreated()

/**
     * Called when the fragment's activity has been created and this
     * fragment's view hierarchy instantiated.  It can be used to do final
     * initialization once these pieces are in place, such as retrieving
     * views or restoring state.  It is also useful for fragments that use
     * {@link #setRetainInstance(boolean)} to retain their instance,
     * as this callback tells the fragment when it is fully associated with
     * the new activity instance.  This is called after {@link #onCreateView}
     * and before {@link #onViewStateRestored(Bundle)}.
     *
     * @param savedInstanceState If the fragment is being re-created from
     * a previous saved state, this is the state.
     */
    public void onActivityCreated(Bundle savedInstanceState) {
        mCalled = true;
    }

 

onDestroyView()

  与onCreateView()相对应

 /**
     * 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()}.  It is called
     * <em>regardless</em> of whether {@link #onCreateView} returned a
     * non-null view.  Internally it is called after the view's state has
     * been saved but before it has been removed from its parent.
     */
    public void onDestroyView() {
        mCalled = true;
    }

 

 

onDetach()

  与onAttach()相对应

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

 

 

接着通过souce Insight查看android.support.v4.app.frament源码

 

posted on 2015-12-08 13:41  右耳Deng  阅读(286)  评论(0编辑  收藏  举报

导航