xml:
Instead of creating a SupportMapFragment in xml, there could be another approach in which we can define container for SupportMapFragment in xml and then load map from class.
In XML, let say container is FrameLayout-
In my java class which is a Fragment, I have created two methods to show and hide map. I used detach and it depends on need. We can also use hide instead of detach.
private void showMap() {
mMapContainer.setVisibility(View.VISIBLE);
mSupportMapFragment = SupportMapFragment.newInstance();
getChildFragmentManager().beginTransaction()
.replace(R.id.mapContainer, mSupportMapFragment).commit();
}
private void hideMap() {
mMapContainer.setVisibility(View.VISIBLE);
if (mSupportMapFragment != null) {
getChildFragmentManager().beginTransaction()
.detach(mSupportMapFragment).commit();
}
}