2

我的应用程序中有一个 MapFragment 和常规片段。问题是当我在片段之间切换时,MapFragment 在后台。我有代码来查找 MapFragment 是否存在,但我需要代码来删除它。

代码:

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");
}
4

3 回答 3

5

尝试这个

@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();
}

于 2014-12-13T09:52:25.490 回答
2

代码将是:

getFragmentManager().beginTransaction().remove(mapFragmentcheck).commit();

请务必阅读文档FragmentTransaction以了解有关如何更改片段的更多信息。

于 2014-01-14T12:32:31.717 回答
0

这对我有用

我正在像这样添加谷歌地图

@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;
}

我在 xml 中的地图看起来像这样

   <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" />

我正在分离这样的片段:-

@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();
        }
    }

请注意,我的 MapView 片段是 android.app.Fragment并且我正在使用getFragmentManager()添加和删除 MapViewFragment。如果您将 V4.Fragment 操作与 app.Fragment 混合使用,您的应用程序将严重崩溃

于 2015-12-18T02:33:22.097 回答