Reputation: 95
I have a MapFragment and regular Fragments in my app. The problem is when I change between the fragments, the MapFragment is in the background. I have code to find if the MapFragment is there, but I need code to remove it.
Code:
FragmentMapView mapFragmentcheck = (FragmentMapView)getFragmentManager().findFragmentByTag("map");
if (mapFragmentcheck != null) {
Log.d("tag","max exists");
if (mapFragmentcheck.isVisible()) {
Log.d("tag","map is visble, remove it");
// Do remove here, but how?
}
}
else {
Log.d("tag","map does not exists");
}
Upvotes: 3
Views: 5312
Reputation: 45052
This worked for me
I was adding Google Map like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment, container, false);
Bundle bdl = getArguments();
// setuping locatiomanager to perfrom location related operations
locationManager = (LocationManager) getActivity().getSystemService(
Context.LOCATION_SERVICE);
// Requesting locationmanager for location updates
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1, 1, this);
// To get map from MapFragment from layout
googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();
// To change the map type to Satellite
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// To show our current location in the map with dot
// googleMap.setMyLocationEnabled(true);
// To listen action whenever we click on the map
googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
/*
* LatLng:Class will give us selected position lattigude and
* longitude values
*/
Toast.makeText(getActivity(), latLng.toString(),
Toast.LENGTH_LONG).show();
}
});
changeMapMode(3);
// googleMap.setSatellite(true);
googleMap.setTrafficEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.setMyLocationEnabled(true);
return v;
}
My map in xml looks like this
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="ScrewMaps" />
I am Detaching Fragments like this :-
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
locationManager.removeUpdates(this);
android.app.Fragment fragment = getActivity().getFragmentManager()
.findFragmentById(R.id.map);
if (null != fragment) {
android.app.FragmentTransaction ft = getActivity()
.getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
Please Note that my MapView Fragment is android.app.Fragment & I am using getFragmentManager() to add and remove MapViewFragment. If you are mixing V4.Fragment operations with app.Fragment your app will crash badly
Upvotes: 0
Reputation: 1501
Try this
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment mapFragment = (MapFragment) getActivity()
.getFragmentManager().findFragmentById(R.id.map_add_place);
if (mapFragment != null)
getActivity().getFragmentManager().beginTransaction()
.remove(mapFragment).commit();
}
Upvotes: 6
Reputation: 17922
The code would be:
getFragmentManager().beginTransaction().remove(mapFragmentcheck).commit();
Make sure to read the documentation on FragmentTransaction
in order to understand more about how to change fragments.
Upvotes: 2