我在TabA中有3个片段,在TabB中有一个PreferenceFragment。要重新创建问题,我启动程序,TabA显示正常。我点击TabB,它也显示正常。如果我返回TabA并进行方向更改,下次单击TabB时,只有一个空白屏幕。我已经将它缩小到FragmentManager而不是在TabB中的Fragment上调用onCreateView。
我正在检查Fragments是否为null并且不需要重新创建并在方向更改后使用findFragmentByTag获取对它们的引用。 TabA从来没有问题,我试图用TabA重新创建问题,但是片段管理器总是会为每个片段调用onCreateView。我有API 15,目标19。
将MyActivity.java的版本修剪为可运行的
public class MyActivity extends Activity {
private static final String TAG = "MainActivity";
public FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
fm = getFragmentManager();
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = actionBar.newTab().setText("Main");
ActionBar.Tab tabB = actionBar.newTab().setText("Settings");
tabA.setTabListener(new TabAListener(this));
actionBar.addTab(tabA);
tabB.setTabListener(new TabBListener(this));
actionBar.addTab(tabB);
if (savedInstanceState != null) {
int index = savedInstanceState.getInt("index");
actionBar.setSelectedNavigationItem(index);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
int i = getActionBar().getSelectedNavigationIndex();
outState.putInt("index", i);
}
public class TabAListener implements ActionBar.TabListener {
private static final String genTag = "GenerateFragment";
private static final String aboutTag = "AboutFragment";
public static final String resultsTag = "ResultsListFragment";
private ArrayList<Fragment> fragList;
public TabAListener(Activity activity) {
fragList = null;
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Reselected don't do anything
Log.d(TAG, "Tab A: on Tab reselected");
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d(TAG, "Tab A: on Tab Selected");
// attach all the fragments
if (fragList == null) {
fragList = new ArrayList<Fragment>();
TestFragment genFrag;
if (fm.findFragmentByTag(genTag) == null) {
genFrag = new TestFragment();
ft.add(R.id.gen_fragment, genFrag, genTag);
} else genFrag = (TestFragment) fm.findFragmentByTag(genTag);
TestFragment aboutFrag;
if (fm.findFragmentByTag(aboutTag) == null) {
aboutFrag = new TestFragment();
ft.add(R.id.about_fragment, aboutFrag, aboutTag);
} else aboutFrag = (TestFragment) fm.findFragmentByTag(aboutTag);
TestFragment resultsFrag;
if (fm.findFragmentByTag(resultsTag) == null) {
resultsFrag = new TestFragment();
ft.add(R.id.results_fragment, resultsFrag, resultsTag);
} else {
resultsFrag = (TestFragment) fm.findFragmentByTag(resultsTag);
}
fragList.add(genFrag);
fragList.add(aboutFrag);
fragList.add(resultsFrag);
Log.d(TAG, "Tab A: Added fragments to the ArrayList");
} else {
Iterator iter = fragList.iterator();
while (iter.hasNext()) {
Log.d(TAG, "Tab A: Attaching fragments");
ft.attach((Fragment) iter.next());
}
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d(TAG, "Tab A: on Tab Unselected");
if (fragList != null) {
Iterator iter = fragList.iterator();
while (iter.hasNext()) {
Log.d(TAG, "Tab A: Fragments detached");
ft.detach((Fragment) iter.next());
}
}
}
}
public class TabBListener implements ActionBar.TabListener {
private static final String settingsTag = "SettingsFragment";
private ArrayList<Fragment> fragList;
public TabBListener(Activity activity) {
fragList = null;
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Reselected don't do anything
Log.d(TAG, "Tab B: on Tab reselected");
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d(TAG, "Tab B: on Tab Selected");
// attach all the fragments
if (fragList == null) {
fragList = new ArrayList<Fragment>();
TestFragment settingsFrag;
if (fm.findFragmentByTag(settingsTag) == null) {
settingsFrag = new TestFragment();
ft.add(R.id.frame_main, settingsFrag, settingsTag);
} else {
Log.d(TAG, "Tab B: not null");
settingsFrag = (TestFragment) fm.findFragmentByTag(settingsTag);
}
fragList.add(settingsFrag);
Log.d(TAG, "Tab B: Added fragments to the ArrayList");
} else {
Iterator iter = fragList.iterator();
while (iter.hasNext()) {
Log.d(TAG, "Tab B: Attaching fragments");
ft.attach((Fragment) iter.next());
}
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d(TAG, "Tab B: on Tab Unselected");
if (fragList != null) {
Iterator iter = fragList.iterator();
while (iter.hasNext()) {
Log.d(TAG, "Tab B: Fragments detached");
ft.detach((Fragment) iter.next());
}
}
}
}
public static class TestFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_text, container, false);
}
}
}
activity_master.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<FrameLayout
android:id="@+id/gen_fragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/about_fragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:id="@+id/results_fragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
</FrameLayout>
fragment_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test" />
</LinearLayout>
答案 0 :(得分:0)
所以,我想我必须删除MainActivity.onSaveInstanceState中的超级调用,以便在方向更改后正确调用TabB&#39; s片段的onCreateView。嗯,我不确定为什么会有效,所以有人可以解释原因吗?
我已经能够收集super.onSaveInstanceState保存活动视图,以便用户导航离开应用程序并最终返回。 FragmentManager还通过方向更改负责片段的内容。那时我有点迷失。