MapView泄漏活动(没有位置服务)

pro*_*m85 6 android google-maps memory-leaks android-mapview

我正在使用播放服务v9.8.0(没有位置服务权限),当我MapView在对话框片段中使用时,我仍然面临泄漏.我在我的代码示例中使用它,我用它来显示一个位置而我没有setMyLocationEnabled(因为我甚至没有这个设置的权限).

有没有人在我的代码中看到问题?我得到的漏洞就像这里的那样:MapView v2保持了Context.我这样做:

  • 创建一个对话框
  • 用a替换我的布局中的视图MapView(因为我也允许使用静态地图,所以我的默认视图ImageView在我的布局中,将替换为a MapView)

然后发生我的碎片泄漏MapView.mContext......

代码 - 对话片段

public class DialogMediaDetails extends DialogFragment
{
    private GoogleMap mGoogleMap = null;
    private MapView mMapView = null;

    @Override
    public final Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Dialog dlg = ...; // create dialog
        View view = ...; // get view from dialog
        Location location = ...; // defined location
        initGoogleMap();
        return dlg;
    }

    private void initGoogleMap(Location location)
    {
        mMapView = new MapView(getActivity());
        MapsInitializer.initialize(getActivity());
        // Updates the location and zoom of the MapView
        mMapView.onCreate(null);
        mMapView.getMapAsync(new OnMapReadyCallback()
        {
            @Override
            public void onMapReady(GoogleMap googleMap)
            {
                LatLng coordinates = new LatLng(location.getLatitude(), location.getLongitude());
                googleMap.addMarker(new MarkerOptions().position(coordinates));
                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
                mGoogleMap = googleMap;
                mMapView.onResume();
            }
        });
        mMapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                mMapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                // MapView is scrollable, so we disable dragging
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
                AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
                behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
                    @Override
                    public boolean canDrag(AppBarLayout appBarLayout) {
                        return false;
                    }
                });
            }
        });

        replaceHeader(mMapView);
    }

    private void replaceHeader(View view)
    {
        ViewGroup parent = (ViewGroup) pbHeader.getParent();
        int index = parent.indexOfChild(pbHeader);
        ViewGroup.LayoutParams lp = pbHeader.getLayoutParams();
        parent.removeView(pbHeader);
        parent.addView(view, index, lp);
    }

    // ----------------------------------------
    // forward all lifecycle events to MapView
    // ----------------------------------------

    @Override
    public void onResume() {
        super.onResume();
        if (mMapView != null)
            mMapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mMapView != null)
            mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mMapView != null)
            mMapView.onPause();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mMapView != null)
            mMapView.onLowMemory();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMapView != null)
            mMapView.onDestroy();
        mMapView = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

abi*_*ita 1

据报告存在一些有关 MapView 泄漏的问题。您可以尝试致电googleMap.setMyLocationEnabled(false);onDestroy防止泄漏发生。未能调用MapView.onDestroyGoogleMap.setMyLocationEnabled(false)将会导致泄漏。这是一个可能有帮助的相关主题