Android MapView v2 Context Issues and Memory Leak

你离开我真会死。 提交于 2019-12-04 16:07:50
hidro

Possibily related to danielgomezrico's answer, there is a confirmed bug related to my location layer in MapView that leaks memory.

The workaround is to make sure you disable my location using GoogleMap.setMyLocationEnabled(false) before the map is destroyed. Also make sure to call other map view's lifecycle methods as well.

private MapView mMapView;
private GoogleMap mMap;

...

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mMap != null) {
        mMap.setMyLocationEnabled(false);
    }
    mMapView.onDestroy();
}

I've been messing with MapView for a few weeks now trying to get it to behave correctly in a ScrollView, to no avail. I'm about to give up on it.

Honestly, that's for the best. MapView is likely never going to play nice inside of a ScrollView. There are a host of bugs that crop up when this combination is used; vertical lag, the MapView rendering over the ActionBar, vertical scrolling simply not working, etc. Removing the ScrollView is not only preferred but most likely required.

That said, if you absolutely must do this, because of client constraints for example, you have two options.

  1. You can use Snapshot & the SnapshotReady callback; when the callback is fired, replace the MapView with an ImageView of the snapshot. You lose interactivity but you also lose the problems you're experiencing. (You're claiming this doesn't work or draws an incomplete bitmap, though I haven't run into that issue. I'm not sure how you're implementing it.)

  2. Write a custom class extending the MapView, override onTouchEvent and for whatever motion events you need to fix (probably ACTION_DOWN and ACTION_UP), manually control whether or not the parent (the ScrollView) can intercept the action. Use something like this.getParent().requestDisallowInterceptTouchEvent(true|false) depending on your case. Check the Android docs for more info on that method.

I hope that helps!

In project that I was working we had similar issue. We were using mapView inside viewholder. Solution was to call onPause and then onDestroy on mapview. After that memory leaks were not observed.

Are you using it inside a fragment? Maybe related to this bug https://code.google.com/p/android/issues/detail?id=185902

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!