Fragments生命周期详解

上一篇文章我详解了Activity的生命周期,今天给大家详解Fragment的生命周期。官方介绍地址:http://developer.android.com/guide/components/fragments.html
此文章同步于我的个人博客网站:http://www.fengchaokm.com/

Fragment生命周期图解:

Fragments生命周期详解_第1张图片

Activity生命周期与Fragment生命周期对比:


下面我们还是通过实例代码来观察Fragment生命周期的详细过程:

public class MainActivity extends Activity {

    static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "Activity*onCreate--------");
        if (getFragmentManager().findFragmentById(android.R.id.content) == null) {  
            ArrayListFragment list = new ArrayListFragment();  
            getFragmentManager().beginTransaction().add(android.R.id.content, list).commit();  
        } 

    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        Log.e(TAG, "Activity*onContentChanged--------");
    }

    public void onStart() {
        super.onStart();
        Log.e(TAG, "Activity*onStart--------");
    }

    public void onRestart() {
        super.onRestart();
        Log.e(TAG, "Activity*onRestart--------");
    }

    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Log.e(TAG, "Activity*onPostCreate--------");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "Activity*onResume--------");
    }

    public void onPostResume() {
        super.onPostResume();
        Log.e(TAG, "Activity*onPostResume--------");
    }

    public void onPause() {
        super.onPause();
        Log.e(TAG, "Activity*onPause--------");
    }

    public void onStop() {
        super.onStop();
        Log.e(TAG, "Activity*onStop--------");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "Activity*onDestroy--------");
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e(TAG, "Activity*onConfigurationChanged--------");
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "Activity*onSaveInstanceState--------");
    }

    public void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        Log.e(TAG, "Activity*onRestoreInstanceState--------");
    }
    public static class ArrayListFragment extends ListFragment {  

        @Override  
        public void onAttach(Activity activity) {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "Fragment* onAttach...");  
            super.onAttach(activity);  
        }  

        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "Fragment* onCreate...");  
            super.onCreate(savedInstanceState);  
        }  

        @Override  
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "Fragment* onCreateView...");  
            return super.onCreateView(inflater, container, savedInstanceState);  
        }  

        @Override  
        public void onActivityCreated(Bundle savedInstanceState) {  
            super.onActivityCreated(savedInstanceState);  
            Log.e(TAG, "Fragment* onActivityCreated...");  
            String[] array = new String[]{"C++", "JAVA", "PYTHON"};  
            setListAdapter(new ArrayAdapter(getActivity(),  
                    android.R.layout.simple_list_item_1, array));  
        }  

        @Override  
        public void onStart() {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "Fragment* onStart...");  
            super.onStart();  
        }  

        @Override  
        public void onResume() {  
            Log.e(TAG, "Fragment* onResume...");  
            // TODO Auto-generated method stub  
            super.onResume();  
        }  

        @Override  
        public void onPause() {  
            Log.e(TAG, "Fragment* onPause...");  
            // TODO Auto-generated method stub  
            super.onPause();  
        }  

        @Override  
        public void onStop() {  
            Log.e(TAG, "Fragment* onStop...");  
            // TODO Auto-generated method stub  
            super.onStop();  
        }  

        @Override  
        public void onDestroyView() {  
            Log.e(TAG, "Fragment* onDestroyView...");  
            // TODO Auto-generated method stub  
            super.onDestroyView();  
        }  

        @Override  
        public void onDestroy() {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "Fragment* onDestroy...");  
            super.onDestroy();  
        }  

        @Override  
        public void onDetach() {  
            Log.e(TAG, "Fragment* onDetach...");  
            // TODO Auto-generated method stub  
            super.onDetach();  
        }  

        @Override  
        public void onListItemClick(ListView l, View v, int position, long id) {  
            Log.i("FragmentList", "Item clicked: " + id);  
        }  
    }  

}

Activity启动时各个生命周期的执行顺序如下:

Fragments生命周期详解_第2张图片

锁屏或按Home键时调用方法执行顺序如下:

Fragments生命周期详解_第3张图片

解锁屏时调用方法执行顺序如下:

Fragments生命周期详解_第4张图片

退出时调用方法执行顺序如下:

Fragments生命周期详解_第5张图片

可以清晰直观的观察出Activity和Fragment生命周期的变化,Fragment的大部分状态和Activity很相似,但Fragment有一些新的状态。

1、onAttached() —— 当fragment被加入到activity时调用(在这个方法中可以获得所在的activity)。

2、onCreateView() —— 当activity要得到fragment的layout时,调用此方法,fragment在其中创建自己的layout(界面)。

3、onActivityCreated() —— 当activity的onCreated()方法返回后调用此方法

4、onDestroyView() —— 当fragment中的视图被移除的时候,调用这个方法。

5、onDetach() —— 当fragment和activity分离的时候,调用这个方法。

当Activity进入运行状态时,我们就可以自由的添加、删除Fragment,因此只有当Activity在运行状态时Fragment的生命周期才能独立的运转,其它时候都是依赖于Activity的生命周期变化。

你可能感兴趣的:(Android)