简体   繁体   English

从另一个的onCreate()方法打开一个新片段

[英]Opening a new fragment from the onCreate() method of another

I'm trying to open a new fragment automatically from the onCreate() method of my original fragment. 我正在尝试从原始片段的onCreate()方法自动打开一个新片段。 I found this code via How do i open a new fragment from another fragment . 我通过如何从另一个片段中打开一个新片段找到了此代码。 The only thing I'm confused about is the resource element. 我唯一感到困惑的是资源元素。 What does R.id.layout refer to in this particular case? 在这种特殊情况下,R.id.layout指的是什么? What is the resource being replaced? 替换的资源是什么? It's asking for the containerViewId. 它要求有containerViewId。 What is that? 那是什么?

    TemplateFragment nextFrag = new TemplateFragment();
    this.getFragmentManager().beginTransaction()
            .replace(R.id.layout, nextFrag, TAG_FRAGMENT)
            .addToBackStack(null)
            .commit();

This is an ID of the layout container where you fragment will be put, ie: 这是将放置片段的布局容器的ID,即:

        <FrameLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

which should exist in your activities layout xml. 这应该存在于您的活动布局xml中。

in method javadoc you have: 在方法javadoc中,您具有:

 * @param containerViewId Identifier of the container whose fragment(s) are * to be replaced. 

What does R.id.layout refer to in this particular case? 在这种特殊情况下,R.id.layout指的是什么? What is the resource being replaced? 替换的资源是什么?

Fragments, when they want to display something, need a container where the result of their onCreateView will be inflated into. 片段要显示某些内容时,需要一个容器,其onCreateView的结果将被放入该容器中。 Of course this container should also be part of the activity currently at screen or you will not see anything 当然,此容器也应该是屏幕上当前活动的一部分,否则您将看不到任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM