相关疑难解决方法(0)

onCreate()和onCreateView()调用的不仅仅是需要的(片段)

有人可以解释为什么onCreate()onCreateView()被调用的次数会随着每个方向的变化而增加吗?

这是一个非常简单的应用程序,由一个Activity由两个组成Fragments.第二个动态Fragment加载.如果您定义这两个中就不会有这样的行为.Fragmentsmain.xml

这是main.xml:

       <fragment class="ets.saeref.Left"
        android:id="@+id/left_frag"
        android:layout_weight="70"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

     <FrameLayout android:id="@+id/right_frag" 
        android:layout_weight="30"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这里留下了碎片:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="#000000">
    <Button android:text="Landscape" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是正确的碎片:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="#ffffff">
    <Button android:text="Landscape" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Left.class:

public class Left extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Left", "onCreate()");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, …
Run Code Online (Sandbox Code Playgroud)

android

46
推荐指数
3
解决办法
7万
查看次数

标签 统计

android ×1