代码很简单,Activity中嵌套一个fragment,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
这种情况,Activity 和 Fragment 是一起创建的,其创建顺序为:
11-30 00:58:02.274 16885-16885/com.example.fragmenttest D/MainActivity: onCreate
11-30 00:58:02.285 16885-16885/com.example.fragmenttest D/Fragment: onAttach
11-30 00:58:02.285 16885-16885/com.example.fragmenttest D/Fragment: onCreate
11-30 00:58:02.285 16885-16885/com.example.fragmenttest D/Fragment: onCreateView
11-30 00:58:02.289 16885-16885/com.example.fragmenttest D/Fragment: onActivityCreated
11-30 00:58:02.289 16885-16885/com.example.fragmenttest D/Fragment: onStart
11-30 00:58:02.289 16885-16885/com.example.fragmenttest D/MainActivity: onStart
11-30 00:58:02.291 16885-16885/com.example.fragmenttest D/MainActivity: onResume
11-30 00:58:02.291 16885-16885/com.example.fragmenttest D/Fragment: onResume
销毁顺序为:
11-30 00:36:47.413 16885-16885/com.example.fragmenttest D/Fragment: onPause
11-30 00:36:47.413 16885-16885/com.example.fragmenttest D/MainActivity: onPause
11-30 00:36:47.637 16885-16885/com.example.fragmenttest D/Fragment: onStop
11-30 00:36:47.637 16885-16885/com.example.fragmenttest D/MainActivity: onStop
11-30 00:36:47.638 16885-16885/com.example.fragmenttest D/Fragment: onDestroyView
11-30 00:36:47.638 16885-16885/com.example.fragmenttest D/Fragment: onDestroy
11-30 00:36:47.638 16885-16885/com.example.fragmenttest D/Fragment: onDetach
11-30 00:36:47.638 16885-16885/com.example.fragmenttest D/MainActivity: onDestroy