Skip to content

Instantly share code, notes, and snippets.

@doneill
Last active May 24, 2022 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doneill/94c6a08b88d9f5fc1ea9e4178d859ff5 to your computer and use it in GitHub Desktop.
Save doneill/94c6a08b88d9f5fc1ea9e4178d859ff5 to your computer and use it in GitHub Desktop.
React Native Android Lifecycle events
// set up lifecycle events in Android Main Activity
private static final String TAG = "MainActivity";
@Override
public void onPause() {
super.onPause();
ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
WritableMap params = Arguments.createMap();
params.putString("event", "onPause");
if(reactContext != null) {
Objects.requireNonNull(getReactInstanceManager().getCurrentReactContext())
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("ActivityStateChange", params);
}
Log.i(TAG, "onPause: ");
}
// Cal from within React Native component
useEffect(() => {
DeviceEventEmitter.addListener('ActivityStateChange', e => {
log.info(`onPause ${e.event}`);
});
return () => DeviceEventEmitter.removeAllListeners();
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment