您好,登錄后才能下訂單哦!
<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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。