activity和fragment生命周期相关

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 眼前 有两条路 一为山路 一为水道 山路坎坷 崇山峻岭遍地 水道汹涌 惊涛骇浪环身 踟蹰 不知何处行 想登高 却惧...
    茫茫沧海中的一叶浮萍阅读 184评论 0 4
  • 时间管理(13/100):日理万机的流程 上图非常清晰的表现了日理万机的流程。 行动三角形:收集~排程~执行 收集...
    小奚之屋阅读 197评论 0 0
  • 落叶它也不知,最终它会去到何处。 秋风携走的季夏的最后一缕炎热,落叶它挣扎着,疲倦而枯黄的身躯在柔风之中瑟瑟发抖,...
    玄机莫探阅读 161评论 0 0
  • 繁华锦绣似烟,玉石珍馐似云。 梦幻兮,梦无期。 但愿不忘初心游离天地。
    澜妖阅读 161评论 0 2