Last active
May 24, 2022 00:18
-
-
Save doneill/94c6a08b88d9f5fc1ea9e4178d859ff5 to your computer and use it in GitHub Desktop.
React Native Android Lifecycle events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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