RCTDeviceEventEmitter which can be obtained from the ReactContext as in the code snippet below.
...
import com.facebook.react.modules.core.DeviceEventManagerModule;
+import com.facebook.react.bridge.WritableMap;
+import com.facebook.react.bridge.Arguments;
...
private void sendEvent(ReactContext reactContext,
String eventName,
@@ -313,42 +317,22 @@ ToastExample.}
...
WritableMap params = Arguments.createMap();
+params.putString("eventProperty", "someValue");
...
-sendEvent(reactContext, "keyboardWillShow", params);
+sendEvent(reactContext, "EventReminder", params);
-JavaScript modules can then register to receive events by addListenerOn using the Subscribable mixin.
import { DeviceEventEmitter } from 'react-native';
+JavaScript modules can then register to receive events by addListener on the NativeEventEmitter class.
+import { NativeEventEmitter, NativeModules } from 'react-native';
...
-var ScrollResponderMixin = {
- mixins: [Subscribable.Mixin],
-
-
componentDidMount() {
...
- this.addListenerOn(DeviceEventEmitter,
- 'keyboardWillShow',
- this.scrollResponderKeyboardWillShow);
+ const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
+ eventEmitter.addListener('EventReminder', (event) => {
+ console.log(event.eventProperty) // "someValue"
+ }
...
- },
- scrollResponderKeyboardWillShow:function(e: Event) {
- this.keyboardWillOpenTo = e;
- this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
- },
-
-You can also directly use the DeviceEventEmitter module to listen for events.
-...
-componentDidMount() {
- this.subscription = DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
- // handle event
- });
-}
-
-componentWillUnmount() {
- // When you want to stop listening to new events, simply call .remove() on the subscription
- this.subscription.remove();
-}
-...
+ }
Getting activity result from startActivityForResult
You'll need to listen to onActivityResult if you want to get results from an activity you started with startActivityForResult. To do this, you must extend BaseActivityEventListener or implement ActivityEventListener. The former is preferred as it is more resilient to API changes. Then, you need to register the listener in the module's constructor,
diff --git a/docs/next/native-modules-android/index.html b/docs/next/native-modules-android/index.html
index dd477b3b43b..fd4dbe5304d 100644
--- a/docs/next/native-modules-android/index.html
+++ b/docs/next/native-modules-android/index.html
@@ -94,12 +94,14 @@
import java.util.HashMap;
public class ToastModule extends ReactContextBaseJavaModule {
+ private static ReactApplicationContext reactContext;
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
- ToastModule(ReactApplicationContext reactContext) {
- super(reactContext);
+ ToastModule(ReactApplicationContext context) {
+ super(context);
+ reactContext = context;
}
}
@@ -303,6 +305,8 @@ ToastExample.Native modules can signal events to JavaScript without being invoked directly. The easiest way to do this is to use the RCTDeviceEventEmitter which can be obtained from the ReactContext as in the code snippet below.
...
import com.facebook.react.modules.core.DeviceEventManagerModule;
+import com.facebook.react.bridge.WritableMap;
+import com.facebook.react.bridge.Arguments;
...
private void sendEvent(ReactContext reactContext,
String eventName,
@@ -313,42 +317,22 @@ ToastExample.}
...
WritableMap params = Arguments.createMap();
+params.putString("eventProperty", "someValue");
...
-sendEvent(reactContext, "keyboardWillShow", params);
+sendEvent(reactContext, "EventReminder", params);
-JavaScript modules can then register to receive events by addListenerOn using the Subscribable mixin.
import { DeviceEventEmitter } from 'react-native';
+JavaScript modules can then register to receive events by addListener on the NativeEventEmitter class.
+import { NativeEventEmitter, NativeModules } from 'react-native';
...
-var ScrollResponderMixin = {
- mixins: [Subscribable.Mixin],
-
-
componentDidMount() {
...
- this.addListenerOn(DeviceEventEmitter,
- 'keyboardWillShow',
- this.scrollResponderKeyboardWillShow);
+ const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
+ eventEmitter.addListener('EventReminder', (event) => {
+ console.log(event.eventProperty) // "someValue"
+ }
...
- },
- scrollResponderKeyboardWillShow:function(e: Event) {
- this.keyboardWillOpenTo = e;
- this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
- },
-
-You can also directly use the DeviceEventEmitter module to listen for events.
-...
-componentDidMount() {
- this.subscription = DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
- // handle event
- });
-}
-
-componentWillUnmount() {
- // When you want to stop listening to new events, simply call .remove() on the subscription
- this.subscription.remove();
-}
-...
+ }
Getting activity result from startActivityForResult
You'll need to listen to onActivityResult if you want to get results from an activity you started with startActivityForResult. To do this, you must extend BaseActivityEventListener or implement ActivityEventListener. The former is preferred as it is more resilient to API changes. Then, you need to register the listener in the module's constructor,