Skip to content

Commit

Permalink
[Android] Lifecycle defaults to focused instead of unfocused (#41875)
Browse files Browse the repository at this point in the history
## Description

In #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 #41702, if no focus events were received, it would default to `inactive`.

## Tests
 - Updated tests to match.
  • Loading branch information
gspencergoog committed May 11, 2023
1 parent d890f23 commit eb88d37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Expand Up @@ -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<String> channel;

Expand Down
Expand Up @@ -31,29 +31,29 @@ public void lifecycleChannel_handlesResumed() {
lifecycleChannel.appIsResumed();
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(mockChannel, times(1)).send(stringArgumentCaptor.capture());
assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());

lifecycleChannel.aWindowIsFocused();
lifecycleChannel.noWindowsAreFocused();
stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(mockChannel, times(2)).send(stringArgumentCaptor.capture());
assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());

lifecycleChannel.noWindowsAreFocused();
lifecycleChannel.aWindowIsFocused();
stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(mockChannel, times(3)).send(stringArgumentCaptor.capture());
assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());

// Stays inactive, so no event is sent.
lifecycleChannel.appIsInactive();
verify(mockChannel, times(3)).send(any(String.class));
verify(mockChannel, times(4)).send(any(String.class));

// Stays inactive, so no event is sent.
lifecycleChannel.appIsResumed();
verify(mockChannel, times(3)).send(any(String.class));
verify(mockChannel, times(5)).send(any(String.class));

lifecycleChannel.aWindowIsFocused();
stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(mockChannel, times(4)).send(stringArgumentCaptor.capture());
verify(mockChannel, times(5)).send(stringArgumentCaptor.capture());
assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
}

Expand Down

0 comments on commit eb88d37

Please sign in to comment.