在我的应用中,我正在使用底部导航。我有5个菜单项,每个菜单项都使用自己的容器。
fragmentManager.beginTransaction().add(R.id.frame_container_account, fragment5, "5").hide(fragment5).commit();
fragmentManager.beginTransaction().add(R.id.frame_container_notification, fragment4, "4").hide(fragment4).commit();
fragmentManager.beginTransaction().add(R.id.frame_container_map, fragment3, "3").hide(fragment3).commit();
fragmentManager.beginTransaction().add(R.id.frame_container_bookmark, fragment2, "2").hide(fragment2).commit();
fragmentManager.beginTransaction().add(R.id.frame_container_marker, fragment1, "1").hide(fragment1).commit();
在fragment1中,我替换了旧的fragment,但一切正常,但是当我尝试在fragment3中打开新的fragment时,它将在其他菜单项(其他容器)上打开
这就是我在fragment1中打开新片段的方法:
mMarkerAdapter.setOnItemClickListener(new MarkerAdapter.OnItemClickListener() {
@Override
public void onItemClick(Marker marker) {
MarkerDetailsFragment markerDetailsFragment = new MarkerDetailsFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("marker", marker);
markerDetailsFragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container_marker, markerDetailsFragment, "MarkerDetailsFragment")
.addToBackStack(null)
.commit();
}
});
这就是我在fragment3中打开新片段的方法:
mClusterManager.setOnClusterItemClickListener(new ClusterManager
.OnClusterItemClickListener<Marker>() {
@Override
public boolean onClusterItemClick(Marker marker) {
MarkerDetailsFragment markerDetailsFragment = new MarkerDetailsFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("marker", marker);
markerDetailsFragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container_map, markerDetailsFragment, "MarkerDetailsFragment")
.addToBackStack(null)
.commit();
return true;
}
});
P.S。 Fragment3仅在frame_container_marker和frame_container_bookmark处打开新片段。在frame_container_notification和frame_container_account中,一切正常。