编辑:经过一些实验,如果我不在 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>