<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