From 96db8b56f64839369c59a2c0448dd05ee846c2ab Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Thu, 11 May 2023 11:08:04 -0700 Subject: [PATCH] [Android] Lifecycle defaults to focused instead of unfocused (flutter/engine#41875) ## Description In https://github.com/flutter/engine/pull/41702, the default state of the focus bit is "false", assuming that Android will send an `onWindowFocusChanged(true)` when the window is first focused, but there appear to be some cases where that doesn't happen. This change puts the initial state back to what it used to be: in the absence of focus change events, entering the "onResume" Android state will report the `resumed` state in Flutter. Before this PR, and after https://github.com/flutter/engine/pull/41702, if no focus events were received, it would default to `inactive`. ## Tests - Updated tests to match. --- .../systemchannels/LifecycleChannel.java | 2 +- .../systemchannels/LifecycleChannelTest.java | 30 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java index f34f774536..6ea0814fe0 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java @@ -30,7 +30,7 @@ public class LifecycleChannel { private String lastAndroidState = ""; private String lastFlutterState = ""; - private boolean lastFocus = false; + private boolean lastFocus = true; @NonNull private final BasicMessageChannel channel; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/LifecycleChannelTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/LifecycleChannelTest.java index e9211d5f3b..3adc80212b 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/LifecycleChannelTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/LifecycleChannelTest.java @@ -31,29 +31,29 @@ public class LifecycleChannelTest { lifecycleChannel.appIsResumed(); ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); verify(mockChannel, times(1)).send(stringArgumentCaptor.capture()); - assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue()); - - lifecycleChannel.aWindowIsFocused(); - stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(mockChannel, times(2)).send(stringArgumentCaptor.capture()); assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue()); lifecycleChannel.noWindowsAreFocused(); stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(mockChannel, times(3)).send(stringArgumentCaptor.capture()); + verify(mockChannel, times(2)).send(stringArgumentCaptor.capture()); assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue()); - // Stays inactive, so no event is sent. - lifecycleChannel.appIsInactive(); - verify(mockChannel, times(3)).send(any(String.class)); - - // Stays inactive, so no event is sent. - lifecycleChannel.appIsResumed(); - verify(mockChannel, times(3)).send(any(String.class)); - lifecycleChannel.aWindowIsFocused(); stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(mockChannel, times(4)).send(stringArgumentCaptor.capture()); + verify(mockChannel, times(3)).send(stringArgumentCaptor.capture()); + assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue()); + + // Stays inactive, so no event is sent. + lifecycleChannel.appIsInactive(); + verify(mockChannel, times(4)).send(any(String.class)); + + // Stays inactive, so no event is sent. + lifecycleChannel.appIsResumed(); + verify(mockChannel, times(5)).send(any(String.class)); + + lifecycleChannel.aWindowIsFocused(); + stringArgumentCaptor = ArgumentCaptor.forClass(String.class); + verify(mockChannel, times(5)).send(stringArgumentCaptor.capture()); assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue()); }