Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

182-disposal-null-ref-crash #259

Merged
merged 6 commits into from
Jul 4, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
Expand Down Expand Up @@ -118,6 +121,11 @@ final class MapboxMapController
private final float density;
private final Context context;
private final String styleStringInitial;
/**
* This container is returned as the final platform view instead of returning `mapView`.
* See {@link MapboxMapController#destroyMapViewIfNecessary()} for details.
*/
private FrameLayout mapViewContainer;
private MapView mapView;
private MapboxMap mapboxMap;
private boolean trackCameraPosition = false;
Expand Down Expand Up @@ -181,6 +189,7 @@ public void onStyleLoaded(@NonNull Style style) {
this.context = context;
this.dragEnabled = dragEnabled;
this.styleStringInitial = styleStringInitial;
this.mapViewContainer = new FrameLayout(context);
this.mapView = new MapView(context, options);
this.interactiveFeatureLayerIds = new HashSet<>();
this.addedFeaturesByLayer = new HashMap<String, FeatureCollection>();
Expand All @@ -190,13 +199,14 @@ public void onStyleLoaded(@NonNull Style style) {
this.androidGesturesManager = new AndroidGesturesManager(this.mapView.getContext(), false);
}

mapViewContainer.addView(mapView);
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/mapbox_maps_" + id);
methodChannel.setMethodCallHandler(this);
}

@Override
public View getView() {
return mapView;
return mapViewContainer;
}

void init() {
Expand Down Expand Up @@ -1544,6 +1554,20 @@ public void onCancel() {
}
}

/**
* Destroy the MapView and cleans up listeners.
* It's very important to call mapViewContainer.removeView(mapView) to make sure
* that {@link TextureView#onDetachedFromWindowInternal()} is called which releases the
* underlying surface.
* This is required due to an FlutterEngine change that was introduce when updating from
* Flutter 2.10.5 to Flutter 3.10.0.
* This FlutterEngine change is not calling `removeView` on a PlatformView which causes the issue.
* <p>
* For more information check out:
* <a href="https://github.com/flutter/flutter/issues/107297">Flutter issue</a>
* <a href="https://github.com/flutter/engine/commit/8dc7cd1b1a33b5da561ac859cdcc49705ad1e598">Flutter Engine commit that introduced the issue</a>
* <a href="https://github.com/maplibre/flutter-maplibre-gl/issues/182">The reported issue in the MapLibre repo</a>
*/
private void destroyMapViewIfNecessary() {
if (mapView == null) {
return;
Expand All @@ -1554,6 +1578,7 @@ private void destroyMapViewIfNecessary() {
}
stopListeningForLocationUpdates();

mapViewContainer.removeView(mapView);
mapView.onDestroy();
mapView = null;
}
Expand Down