5

编辑:经过一些实验,如果我不在 xml 布局中添加初始片段,它看起来像预期的那样工作。我现在正在我的活动源代码中执行此操作。我想这就是我期望的方式吗?

根据http://developer.android.com/guide/components/fragments.html#Creating,如果一个片段被删除然后又添加回来, onCreateView() 应该被调用。

我还可以看到 getView() 返回 null。onDestroyView() 被调用但我的第一个片段的界面在按下返回时仍然显示

这是我的示例代码的结果:

--launch app
I/System.out( 3765): ==== FRAGMENT1.ONCREATE
I/System.out( 3765): ==== FRAGMENT1.ONCREATEVIEW
I/System.out( 3765): ==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = android.support.v4.app.NoSaveStateFrameLayout@41301268

--second fragment
I/System.out( 3765): ==== FRAGMENT2.ONCREATE
I/System.out( 3765): ==== FRAGMENT2.ONCREATEVIEW
I/System.out( 3765): ==== FRAGMENT2.ONACTIVITYCREATED

--back is pressed : getView() == null and onCreateView is not called
I/System.out( 3765): ==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = null

如果我做错了什么,这里有一些基本代码来重现我的问题:

我的 2 个片段类:

package com.test.testbackfragment;

import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Fragment1 extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        System.out.println("==== FRAGMENT1.ONCREATE");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
        System.out.println("==== FRAGMENT1.ONCREATEVIEW");
        View v = inflater.inflate(R.layout.fragment1, container, false);

        v.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    getActivity().getSupportFragmentManager().beginTransaction()
                        .remove(Fragment1.this)
                        .add(R.id.fragment, new Fragment2())
                        .addToBackStack(null)
                        .commit();
                }
            });
        return v;
    }        

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = "+getView());
    }    
}

package com.test.testbackfragment;

import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Fragment2 extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        System.out.println("==== FRAGMENT2.ONCREATE");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
        System.out.println("==== FRAGMENT2.ONCREATEVIEW");
        View v = inflater.inflate(R.layout.fragment2, container, false);
        return v;
    }        

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("==== FRAGMENT2.ONACTIVITYCREATED");
    }    
}

主要活动很简单

package com.test.testbackfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

这里有 3 种布局:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    <fragment android:name="com.test.testbackfragment.Fragment1"
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

片段1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="#0000FF">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:text="THIS IS FRAGMENT1" />

    <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:textSize="20dip"
            android:text="FRAGMENT2"/>
</LinearLayout>

片段2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="#00FF00">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:text="THIS IS FRAGMENT2" />
</LinearLayout>
4

2 回答 2

1

当系统创建这个 main.xml 布局时,它会实例化布局中指定的片段并调用 onCreateView() 方法,以检索每个片段的布局。稍后它将使用相同的片段实例。因此,它不会再次调用 OnCreateView。

如果您想再次创建,请从您的 Activity 中获取 FragmentTransaction 的实例并根据需要实现。

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction .replace(R.id.fragment, newFragment);
// Commit the transaction
transaction.commit();
于 2014-09-10T05:17:50.887 回答
0

尝试FragmentManager使用 FragmentTransaction.remove(your fragment)beforeBackPress事件从堆栈中删除片段。

或仅使用FragmentTransaction.replace()加载新片段。

于 2014-11-15T07:49:04.553 回答