删除MapFragment

3

我在我的应用程序中有一个MapFragment和常规Fragments。问题是当我在不同的Fragments之间切换时,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");
}
3个回答

6

试试这个

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


1
谢谢。在许多其他高票问题中都找不到这个简单的答案。 - Dewsworld

2
代码应该是这样的:
getFragmentManager().beginTransaction().remove(mapFragmentcheck).commit();

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


不用谢。顺便说一下:你不需要在已解决的问题上添加[SOLVED]。只需接受最有帮助的答案即可。这个网站会处理其余的事情。高接受率也会增加人们回答你问题的概率。 - Ben Weiss

0

这对我有用

我是这样添加Google地图的

@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 Fragment是 android.app.Fragment,我正在使用 getFragmentManager() 来添加和删除MapViewFragment。如果您混合使用V4.Fragment操作和app.Fragment,您的应用程序将崩溃严重。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接