React native + Android Activity onPause / onResume

I have js code with the following emitter:

DeviceEventEmitter.addListener('keyboardWillShow1', function(e: Event) { console.log(e); }); 

How can I emit this event from Activity onPause / onResume ?

+5
source share
2 answers

You can send an event from java using the RCTDeviceEventEmitter.emit method defined here: DeviceEventManagerModule.java # L27

To do this, you first need to access the ReactApplicationContext , and then call:

 reactAppContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit("keyboardWillShow1", null); 

Instead of null, you can send arbitrary data, which will then be attached to the event that you receive on the JS side.

See this DeviceEventManagerModule.java # L49 for an example - this is how back button events are sent to JS.

Then you can use a similar template to send events from onPause / onResume , provided that you have a link to ReactApplicationContext

Another way is to create your own custom module that can register to receive life cycle events. See how this is done in the Timing module:

  • The Timing module implements the LifecycleEventListener.java interface
  • When a module is initialized, it registers to receive the life cycle through this interface Timing.java # L126
  • You can implement the onHostPause and onHostResume this interface and use the fragment from the above to send events from there
+5
source

I believe the react-native-activity-android module does this.

+1
source

Source: https://habr.com/ru/post/1236158/


All Articles