我正在使用FrameLayouts来支持Activity加载不同的片段。第一个片段是MapFragment,另一个是MapDownloadManagerFragment用于地图。
我有关于popBackStack()方法的问题。 MapFragment不显示任何地图。它在popBackStack()执行后只显示白色背景。
以下是代码:
private MapFragment mMapFragment = null;
private FragmentTransaction mFragmentTransaction;
private MapDownloadManagerFragment mMapDownloadManagerFragment;
protected void onCreate(Bundle savedInstanceState) {
if (mMapFragment == null) {
mMapFragment = new MapFragment();
}
mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.add(R.id.main_frame_layout, mMapFragment, mFragmentID);
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.commit();
}
private void onClickMenutItem() {
mMapDownloadManagerFragment = new MapDownloadManagerFragment();
mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.replace(R.id.main_frame_layout, mMapDownloadManagerFragment, mdmFragmentID);
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
}
@Override
public void onBackPressed() {
Log.d(TAG, "Back stack count " + getFragmentManager().getBackStackEntryCount());
if( getFragmentManager().getBackStackEntryCount() == 0) {
finish();
} else {
getFragmentManager().popBackStack();
}
}
有没有人遇到过这个问题?我需要你的帮助。提前谢谢。
答案 0 :(得分:2)
现在我通过删除当前的MapFragment并用新的MapFragment替换它来修复此问题。
mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.remove(mMapFragment);
mMapFragment = null;
mMapFragment = new MapFragment();
mFragmentTransaction.add(R.id.main_frame_layout, mMapFragment, mFragmentID);
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.commit();
仍在思考如何以最佳方式实施它。