我正在使用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(); } }
有没有人遇到这个问题?我需要你的帮助。提前致谢。
最佳答案
现在,我通过删除当前的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();
仍在考虑如何以最佳方式实施它。