美文网首页
activity和fragment生命周期相关

activity和fragment生命周期相关

作者: 有点健忘 | 来源:发表于2018-06-12 09:48 被阅读5次

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

相关文章

网友评论

      本文标题:activity和fragment生命周期相关

      本文链接:https://www.haomeiwen.com/subject/rghrfftx.html