Skip to content

Feature to listen on window focus events#25039

Closed
krizzu wants to merge 2 commits into
facebook:masterfrom
krizzu:appstate_focusChange
Closed

Feature to listen on window focus events#25039
krizzu wants to merge 2 commits into
facebook:masterfrom
krizzu:appstate_focusChange

Conversation

@krizzu
Copy link
Copy Markdown

@krizzu krizzu commented May 25, 2019

Summary

Addressed issue: #24149

On Android, activity's lifecycle events are not triggered when the user pulls down the Status Bar (opening Notification Drawer). In order to know that, you need to override onWindowFocusChanged method.

Changelog

[Android] [Added] - Adds a new listener for onWindowFocusChanged
[JavaScript] [Added] - New event, focusChanged, to listen on focus gain/loss

Test Plan

AppState.addEventListener("focusChanged", hasFocus => {
   if(hasFocus) {
       // do something while we have focus
   } else {
       // unfocused
   }
});

appfocusg

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner labels May 25, 2019
Copy link
Copy Markdown

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code analysis results:

  • eslint found some issues. Run yarn lint --fix to automatically fix problems.

@react-native-bot react-native-bot added Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature. labels May 25, 2019
@krizzu krizzu force-pushed the appstate_focusChange branch from 522c8a2 to 0d7502f Compare May 25, 2019 17:40
mWindowFocusEventListeners.add(listener);
}

public void removeWindowFocusChangeListener(WindowFocusChangeListener listener) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeWindowFocusChangeListener is never invoked

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, I left it there in case I've missed a place where I should remove all listeners

@krizzu krizzu changed the title adds feature to listen on focus event changes Feature to listen on window focus events May 28, 2019
@matthargett
Copy link
Copy Markdown
Contributor

can this be exposed as an onfocus and onblur event, to match web?
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onblur

@krizzu
Copy link
Copy Markdown
Author

krizzu commented May 29, 2019

@matthargett Do you mean to have two separate events?

@cpojer
Copy link
Copy Markdown
Contributor

cpojer commented May 31, 2019

@krizzu yes, let's mirror web.

@krizzu
Copy link
Copy Markdown
Author

krizzu commented Jun 1, 2019

@cpojer @matthargett yup, done!

Copy link
Copy Markdown
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The events should be called focus and blur, without the on part though. Could you make that change?

Copy link
Copy Markdown
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, meant to request changes.

@krizzu krizzu force-pushed the appstate_focusChange branch from 3c273f6 to 17c6cec Compare June 5, 2019 04:14
Copy link
Copy Markdown
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @krizzu in d45818f.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Jun 5, 2019
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this pull request Mar 10, 2020
Summary:
Addressed issue: facebook#24149

On Android, activity's lifecycle events are not triggered when the user pulls down the Status Bar (opening Notification Drawer). In order to know that, you need to override [onWindowFocusChanged method](https://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged(boolean)).

## Changelog

[Android] [Added] - Adds a new listener for `onWindowFocusChanged`
[JavaScript] [Added] - New event, `focusChanged`, to listen on focus gain/loss
Pull Request resolved: facebook#25039

Differential Revision: D15644954

Pulled By: cpojer

fbshipit-source-id: 823acffc4287bec4bf56e9f5ffcac65c01cf13d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Callstack Partner: Callstack Partner Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants