即使用户导航到另一个无地图片段,Google Maps API也会运行请求

时间:2014-11-22 14:24:13

标签: android google-maps android-fragments

在我的应用中,我有一个带有Google Map API V2的屏幕(片段) 如果我打开此屏幕,然后打开导航抽屉并单击并转到应用程序的其他位置,在接下来的2-3秒内,我可以看到地图继续在后台执行某些请求。

如何阻止这些片段在片段的onPause / onDestroy中运行?

enter image description here

以下是地图代码的一部分:

mapFragment = (SupportMapFragment)     getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    googleMap = mapFragment.getMap();
    MapsInitializer.initialize(getActivity());
    googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
        public boolean onMarkerClick(Marker arg0) {

            cameraUpdateAction = CameraUpdateAction.CAMERA_ANIMATE;
            return false;

        }
    });

    googleMap.getUiSettings().setZoomControlsEnabled(false);
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    googleMap.setOnCameraChangeListener(onCameraChangeListener());

1 个答案:

答案 0 :(得分:1)

MapFragment documentation开始,它指出Any objects obtained from the GoogleMap is associated with the view. It's important to not hold on to objects (e.g. Marker) beyond the view's life. Otherwise it will cause a memory leak as the view cannot be released.

您可以尝试在MapFragment的onPause()/ onDestroy()方法中调用GoogleMap的clear()方法。