Activity與Fragment的生命週期測試

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".MainActivity">

    <fragment
        android:name="com.cstar.androidstudy.FragPageOne"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        tools:layout="@layout/frag_page_1"/>

    <fragment
        android:name="com.cstar.androidstudy.FragPageTwo"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        tools:layout="@layout/frag_page_2" />
</LinearLayout>

一個Activity中放入2個Fragment,然後測試Activity和2個Fragment的生命週期

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onCreate

1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onAttach!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onCreate!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onCreateView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onViewCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onAttach
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onCreate!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onCreateView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onViewCreated!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onActivityCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onActivityCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStart!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onResume!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onPause!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStop!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onRestart!
1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStart!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onResume!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onPause!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStop!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDestroyView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDetach!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDestroyView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDetach!

註解:

1、主Activity onCreate後纔開始依次初始化每個Fragment,每個Fragment先onAttach,然後onCreate,onCreateView,onViewCreated,這裏值得注意的是,與Activity不同,Fragment的onCreate中,並沒有創建、呈現子組件,所以在onCreate中,是無法訪問子組件的。在onCreateView運行完成後,Fragment中才創建添加了子組件。所以至少要再onViewCreated中,才能用 fragment.getView().findViewById(R.id.xxx)獲取子組件的引用。


2、主Activity中所有Fragment依次執行完onViewCreated之後,Activity才onStart開始顯示界面。這時每個Fragment會調用onActivityCreated,僅僅在Activity初次onStart時,每個Fragment纔會調用onActivityCreated,Activity以後再次調用onStart,Fragment不會再調用onActivityCreated(根據onActivityCreated的名稱,這是可想而知的)。如果某個Fragment中的組件想訪問其他Fragment中組件的數據,那麼必須等到該Fragment調用onActivityCreated時,才能保證其他所有Fragment都已創建了子組件,在onActivityCreated之前,很可能會因爲別的Fragment還沒有初始化創建子組件而導致findViewById返回null。每個Fragment執行onActivityCreated後,會執行onStart。


3、主Activity分別執行onResume,onPause,onStop之後,每個Fragment也會跟着Activity之後依次執行同名的方法,即Activity.onResume——>Fragment1.onResume——>Fragment2.onResume……。但Fragment沒有onRestart方法,主Activity重新回到棧頂顯示界面,執行onRestart,onStart,每個Fragment依次執行onStart(沒有onRestart)


4、主Activity退出時,Activity和每個Fragment依次執行onPause,onStop。最後主Activity調用onDestroy。每個Fragment依次調用onDestroyView,onDestroy,onDetach,注意Fragment最後的回調方法是onDetach而不是onDestroy

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章