diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 4515aa46c3..185e03a112 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -232,14 +232,22 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega Log.e(TAG, "Disposing unknown platform view with id: " + viewId); return; } + if (platformView.getView() != null) { + final View embeddedView = platformView.getView(); + final ViewGroup pvParent = (ViewGroup) embeddedView.getParent(); + if (pvParent != null) { + // Eagerly remove the embedded view from the PlatformViewWrapper. + // Without this call, we see some crashes because removing the view + // is used as a signal to stop processing. + pvParent.removeView(embeddedView); + } + } platformViews.remove(viewId); - try { platformView.dispose(); } catch (RuntimeException exception) { Log.e(TAG, "Disposing platform view threw an exception", exception); } - if (usesVirtualDisplay(viewId)) { final VirtualDisplayController vdController = vdControllers.get(viewId); final View embeddedView = vdController.getView(); @@ -581,7 +589,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega // Configures the view for Texture Layer Hybrid Composition mode, returning the associated // texture ID. @TargetApi(23) - private long configureForTextureLayerComposition( + @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) + public long configureForTextureLayerComposition( @NonNull PlatformView platformView, @NonNull PlatformViewsChannel.PlatformViewCreationRequest request) { // This mode attaches the view to the Android view hierarchy and record its drawing diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java index 09781b21ce..8b49503d71 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java @@ -39,6 +39,7 @@ import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.systemchannels.AccessibilityChannel; import io.flutter.embedding.engine.systemchannels.MouseCursorChannel; import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel; +import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel.PlatformViewTouch; import io.flutter.embedding.engine.systemchannels.SettingsChannel; import io.flutter.embedding.engine.systemchannels.TextInputChannel; import io.flutter.plugin.common.MethodCall; @@ -79,6 +80,8 @@ public class PlatformViewsControllerTest { @Override public void dispose() { + // We have been removed from the view hierarhy before the call to dispose. + assertNull(view.getParent()); disposeCalls++; } @@ -98,6 +101,46 @@ public class PlatformViewsControllerTest { } } + @Test + @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) + public void itRemovesPlatformViewBeforeDiposeIsCalled() { + PlatformViewsController platformViewsController = new PlatformViewsController(); + FlutterJNI jni = new FlutterJNI(); + attach(jni, platformViewsController); + // Get the platform view registry. + PlatformViewRegistry registry = platformViewsController.getRegistry(); + + // Register a factory for our platform view. + registry.registerViewFactory( + CountingPlatformView.VIEW_TYPE_ID, + new PlatformViewFactory(StandardMessageCodec.INSTANCE) { + @Override + public PlatformView create(Context context, int viewId, Object args) { + return new CountingPlatformView(context); + } + }); + + // Create the platform view. + int viewId = 0; + final PlatformViewsChannel.PlatformViewCreationRequest request = + new PlatformViewsChannel.PlatformViewCreationRequest( + viewId, + CountingPlatformView.VIEW_TYPE_ID, + 0, + 0, + 128, + 128, + View.LAYOUT_DIRECTION_LTR, + null); + PlatformView pView = platformViewsController.createPlatformView(request, true); + assertTrue(pView instanceof CountingPlatformView); + CountingPlatformView cpv = (CountingPlatformView) pView; + platformViewsController.configureForTextureLayerComposition(pView, request); + assertEquals(0, cpv.disposeCalls); + platformViewsController.disposePlatformView(viewId); + assertEquals(1, cpv.disposeCalls); + } + @Test @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) public void itNotifiesPlatformViewsOfEngineAttachmentAndDetachment() { @@ -121,7 +164,7 @@ public class PlatformViewsControllerTest { int viewId = 0; final PlatformViewsChannel.PlatformViewCreationRequest request = new PlatformViewsChannel.PlatformViewCreationRequest( - viewId++, + viewId, CountingPlatformView.VIEW_TYPE_ID, 0, 0,