activity加载fragment有两种方法
第一种,在oncreate里添加
setContentView(R.layout.activity_life1);
if(arg0!=null) {
}else {
getSupportFragmentManager().beginTransaction().replace(R.id.layout_container, new FragmentLife1(),"fragmentlife1").commitAllowingStateLoss();
}
这种情况下生命周期如下
activity的onCreate
------------onStart
fragment的onInflate
-------------onCreate
-------------onCreateView
--------------onViewCreated
--------------onActivityCreated
--------------onStart
activity的onResume
fragment的onResume
2第二种,直接在布局文件里添加fragment的
<fragment
android:id="@+id/fragment2"
android:name="testlifecycle.FragmentLife2"
android:label="@string/app_name"
android:color="#0000ff"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这种情况下生命周期如下
activity的onCreate
然后在setContentView(R.layout.activity_life1);这里就会加载fragment的
所以接下来就是
fragment的onInflate
-------------onCreate
-------------onCreateView
--------------onViewCreated
然后继续activity的onCreate里setContentView之后的代码,
然后activity的onStart
然后fragment的
--------------onActivityCreated
--------------onStart
activity的onResume
fragment的onResume
网友评论