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 9c78a3f0301..e585a1392b6 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 @@ -1115,14 +1115,13 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega * testing. */ @VisibleForTesting - void initializePlatformViewIfNeeded(int viewId) { + boolean initializePlatformViewIfNeeded(int viewId) { final PlatformView platformView = platformViews.get(viewId); if (platformView == null) { - throw new IllegalStateException( - "Platform view hasn't been initialized from the platform view channel."); + return false; } if (platformViewParent.get(viewId) != null) { - return; + return true; } final View embeddedView = platformView.getView(); if (embeddedView == null) { @@ -1160,6 +1159,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega parentView.addView(embeddedView); flutterView.addView(parentView); + return true; } public void attachToFlutterRenderer(@NonNull FlutterRenderer flutterRenderer) { @@ -1189,7 +1189,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega int viewHeight, @NonNull FlutterMutatorsStack mutatorsStack) { initializeRootImageViewIfNeeded(); - initializePlatformViewIfNeeded(viewId); + if (!initializePlatformViewIfNeeded(viewId)) { + return; + } final FlutterMutatorView parentView = platformViewParent.get(viewId); parentView.readyToDisplay(mutatorsStack, x, y, width, height); 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 af3a79e7adc..310f5fe051f 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 @@ -461,7 +461,7 @@ public class PlatformViewsControllerTest { // Simulate create call from the framework. createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true); - platformViewsController.initializePlatformViewIfNeeded(platformViewId); + assertTrue(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); View resultAndroidView = platformViewsController.getPlatformViewById(platformViewId); assertNotNull(resultAndroidView); @@ -647,11 +647,7 @@ public class PlatformViewsControllerTest { jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ false); assertEquals(ShadowFlutterJNI.getResponses().size(), 1); - assertThrows( - IllegalStateException.class, - () -> { - platformViewsController.initializePlatformViewIfNeeded(platformViewId); - }); + assertFalse(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); } @Test @@ -675,11 +671,7 @@ public class PlatformViewsControllerTest { createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true); assertEquals(ShadowFlutterJNI.getResponses().size(), 1); - assertThrows( - IllegalStateException.class, - () -> { - platformViewsController.initializePlatformViewIfNeeded(platformViewId); - }); + assertFalse(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); } @Test @@ -901,7 +893,7 @@ public class PlatformViewsControllerTest { // Simulate create call from the framework. createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true); - platformViewsController.initializePlatformViewIfNeeded(platformViewId); + assertTrue(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); assertNotNull(androidView.getParent()); assertTrue(androidView.getParent() instanceof FlutterMutatorView); @@ -912,7 +904,7 @@ public class PlatformViewsControllerTest { // Simulate create call from the framework. createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true); - platformViewsController.initializePlatformViewIfNeeded(platformViewId); + assertTrue(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); assertNotNull(androidView.getParent()); assertTrue(androidView.getParent() instanceof FlutterMutatorView); @@ -941,7 +933,7 @@ public class PlatformViewsControllerTest { // Simulate create call from the framework. createPlatformView( jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ false); - platformViewsController.initializePlatformViewIfNeeded(platformViewId); + assertTrue(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); when(platformView.getView()).thenReturn(null); @@ -1080,7 +1072,7 @@ public class PlatformViewsControllerTest { // Simulate create call from the framework. createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true); - platformViewsController.initializePlatformViewIfNeeded(platformViewId); + assertTrue(platformViewsController.initializePlatformViewIfNeeded(platformViewId)); assertEquals(flutterView.getChildCount(), 2); // Simulate first frame from the framework.