diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java index 53cc25e177..dd0026c3ba 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -1522,6 +1522,17 @@ public class FlutterView extends FrameLayout textInputPlugin.autofill(values); } + @Override + public void setVisibility(int visibility) { + super.setVisibility(visibility); + // For `FlutterSurfaceView`, setting visibility to the current `FlutterView` will not take + // effect since it is not in the view tree. So override this method and set the surfaceView. + // See https://github.com/flutter/flutter/issues/105203 + if (renderSurface instanceof FlutterSurfaceView) { + ((FlutterSurfaceView) renderSurface).setVisibility(visibility); + } + } + /** * Listener that is notified when a {@link io.flutter.embedding.engine.FlutterEngine} is attached * to/detached from a given {@code FlutterView}. diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java index 6fb840f3c9..9310b3ae65 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java @@ -1061,6 +1061,31 @@ public class FlutterActivityAndFragmentDelegateTest { assertEquals(View.GONE, delegate.flutterView.getVisibility()); } + @Test + public void flutterSurfaceViewVisibilityChangedWithFlutterView() { + // ---- Test setup ---- + // Create the real object that we're testing. + FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost); + delegate.onAttach(ctx); + delegate.onCreateView(null, null, null, 0, true); + // --- Execute the behavior under test --- + // For `FlutterSurfaceView`, setting visibility to the current `FlutterView` will not take + // effect since it is not in the view tree. So we need to make sure that when the visibility of + // `FlutterView` changes, the `FlutterSurfaceView` changes at the same time + // See https://github.com/flutter/flutter/issues/105203 + assertEquals(FlutterSurfaceView.class, delegate.flutterView.renderSurface.getClass()); + FlutterSurfaceView surfaceView = (FlutterSurfaceView) delegate.flutterView.renderSurface; + // Verify that the `FlutterSurfaceView` is gone. + delegate.flutterView.setVisibility(View.GONE); + assertEquals(View.GONE, surfaceView.getVisibility()); + // Verify that the `FlutterSurfaceView` is visible. + delegate.flutterView.setVisibility(View.VISIBLE); + assertEquals(View.VISIBLE, surfaceView.getVisibility()); + // Verify that the `FlutterSurfaceView` is invisible. + delegate.flutterView.setVisibility(View.INVISIBLE); + assertEquals(View.INVISIBLE, surfaceView.getVisibility()); + } + @Test public void itDoesNotDelayTheFirstDrawWhenRequestedAndWithAProvidedSplashScreen() { when(mockHost.provideSplashScreen())