Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ed3704c

Browse files
Change visibility of FlutterView when onStop/onStart (#30897)
1 parent 25b8d10 commit ed3704c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
// to this FlutterActivityAndFragmentDelegate.
7575
@NonNull private Host host;
7676
@Nullable private FlutterEngine flutterEngine;
77-
@Nullable private FlutterView flutterView;
77+
@VisibleForTesting @Nullable FlutterView flutterView;
7878
@Nullable private PlatformPlugin platformPlugin;
7979
@VisibleForTesting @Nullable OnPreDrawListener activePreDrawListener;
8080
private boolean isFlutterEngineFromHost;
@@ -388,6 +388,12 @@ void onStart() {
388388
Log.v(TAG, "onStart()");
389389
ensureAlive();
390390
doInitialFlutterViewRun();
391+
// This is a workaround for a bug on some OnePlus phones. The visibility of the application
392+
// window is still true after locking the screen on some OnePlus phones, and shows a black
393+
// screen when unlocked. We can work around this by changing the visibility of FlutterView in
394+
// onStart and onStop.
395+
// See https://github.com/flutter/flutter/issues/93276
396+
flutterView.setVisibility(View.VISIBLE);
391397
}
392398

393399
/**
@@ -574,6 +580,12 @@ void onStop() {
574580
Log.v(TAG, "onStop()");
575581
ensureAlive();
576582
flutterEngine.getLifecycleChannel().appIsPaused();
583+
// This is a workaround for a bug on some OnePlus phones. The visibility of the application
584+
// window is still true after locking the screen on some OnePlus phones, and shows a black
585+
// screen when unlocked. We can work around this by changing the visibility of FlutterView in
586+
// onStart and onStop.
587+
// See https://github.com/flutter/flutter/issues/93276
588+
flutterView.setVisibility(View.GONE);
577589
}
578590

579591
/**

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.graphics.Color;
2222
import android.graphics.drawable.ColorDrawable;
2323
import android.net.Uri;
24+
import android.view.View;
2425
import androidx.annotation.NonNull;
2526
import androidx.lifecycle.Lifecycle;
2627
import io.flutter.FlutterInjector;
@@ -484,6 +485,7 @@ public void itForwardsOnRequestPermissionsResultToFlutterEngine() {
484485
// --- Execute the behavior under test ---
485486
// The FlutterEngine is set up in onAttach().
486487
delegate.onAttach(RuntimeEnvironment.application);
488+
delegate.onCreateView(null, null, null, 0, true);
487489
// Emulate app start.
488490
delegate.onStart();
489491

@@ -511,6 +513,7 @@ public void itForwardsOnRequestPermissionsResultToFlutterEngine() {
511513
// --- Execute the behavior under test ---
512514
// The FlutterEngine is set up in onAttach().
513515
delegate.onAttach(RuntimeEnvironment.application);
516+
delegate.onCreateView(null, null, null, 0, true);
514517
// Emulate app start.
515518
delegate.onStart();
516519

@@ -538,6 +541,7 @@ public void itForwardsOnRequestPermissionsResultToFlutterEngine() {
538541
// --- Execute the behavior under test ---
539542
// The FlutterEngine is set up in onAttach().
540543
delegate.onAttach(RuntimeEnvironment.application);
544+
delegate.onCreateView(null, null, null, 0, true);
541545
// Emulate app start.
542546
delegate.onStart();
543547

@@ -565,6 +569,7 @@ public void itForwardsOnRequestPermissionsResultToFlutterEngine() {
565569
// --- Execute the behavior under test ---
566570
// The FlutterEngine is set up in onAttach().
567571
delegate.onAttach(RuntimeEnvironment.application);
572+
delegate.onCreateView(null, null, null, 0, true);
568573
// Emulate app start.
569574
delegate.onStart();
570575

@@ -590,6 +595,7 @@ public void itSendsdefaultInitialRouteOnStartIfNotDeepLinkingFromIntent() {
590595
// --- Execute the behavior under test ---
591596
// The FlutterEngine is set up in onAttach().
592597
delegate.onAttach(RuntimeEnvironment.application);
598+
delegate.onCreateView(null, null, null, 0, true);
593599
// Emulate app start.
594600
delegate.onStart();
595601

@@ -978,6 +984,26 @@ public void itThrowsWhenDelayingTheFirstDrawAndUsingATextureView() {
978984
});
979985
}
980986

987+
@Test
988+
public void itChangesFlutterViewVisibilityWhenOnStartAndOnStop() {
989+
// ---- Test setup ----
990+
// Create the real object that we're testing.
991+
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
992+
993+
// --- Execute the behavior under test ---
994+
delegate.onAttach(RuntimeEnvironment.application);
995+
delegate.onCreateView(null, null, null, 0, true);
996+
delegate.onStart();
997+
// Verify that the flutterView is visible.
998+
assertEquals(View.VISIBLE, delegate.flutterView.getVisibility());
999+
delegate.onStop();
1000+
// Verify that the flutterView is not visible.
1001+
assertEquals(View.GONE, delegate.flutterView.getVisibility());
1002+
delegate.onStart();
1003+
// Verify that the flutterView is visible.
1004+
assertEquals(View.VISIBLE, delegate.flutterView.getVisibility());
1005+
}
1006+
9811007
@Test
9821008
public void itDoesNotDelayTheFirstDrawWhenRequestedAndWithAProvidedSplashScreen() {
9831009
when(mockHost.provideSplashScreen())

0 commit comments

Comments
 (0)