MapView Activity Activity (no location service)

I use playback services v9.8.0 ( without permissions to access the service ), and I still encounter a leak when I use MapView in a dialog fragment. I use it, as in my code example, and use it to display only the location, and I do not have setMyLocationEnabled (since I do not even have permissions for this setting).

Does anyone see a problem in my code? I get a leak like here: MapView v2 supporting context . I do the following:

  • create dialogue
  • replace the view in my layout with MapView (because I also allow the use of static maps, so my default image is the ImageView in my layout, which will be replaced with a MapView )

Then it happens that my fragments leak out MapView.mContext ...

Code - a fragment of the dialogue

 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; } } 
+6
source share
2 answers

There are some issues that report leaks with MapView. You can try calling googleMap.setMyLocationEnabled(false); in onDestroy to prevent leakage. Failure to call MapView.onDestroy or GoogleMap.setMyLocationEnabled(false) will result in a leak. Here is a related thread that might help.

+1
source

I had the same problem, and I realized in the heap link tree that the error was coming from part of the Lite mode in the library. Do not try to use Lite mode and adding all the appropriate life cycles for mapview.

0
source

Source: https://habr.com/ru/post/1012295/


All Articles