Skip to content

Commit

Permalink
Reinstate RCTDeviceEventEmitter section for Android docs
Browse files Browse the repository at this point in the history
Summary:
An earlier diff removed the "Sending Events to JavaScript" section of
the Native Modules (Android) docs.

Previous diff:
30bf039#diff-bdf570846f463516068b23131b72eaaf

Adding that section back in.

Confirm section renders correctly via markdown.
Closes #14143

Differential Revision: D5116899

Pulled By: hramos

fbshipit-source-id: c4a0f2a7fa8c5b1a0386c3a4e83d3ec2cd5c247e
  • Loading branch information
Ian Hill authored and facebook-github-bot committed May 23, 2017
1 parent bec9f41 commit 98c8d62
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/NativeModulesAndroid.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,60 @@ measureLayout();
Native modules should not have any assumptions about what thread they are being called on, as the current assignment is subject to change in the future. If a blocking call is required, the heavy work should be dispatched to an internally managed worker thread, and any callbacks distributed from there.
### Sending Events to JavaScript
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.
```java
...
private void sendEvent(ReactContext reactContext,
String eventName,
@Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
...
WritableMap params = Arguments.createMap();
...
sendEvent(reactContext, "keyboardWillShow", params);
```
JavaScript modules can then register to receive events by `addListenerOn` using the `Subscribable` mixin.
```js
import { DeviceEventEmitter } from 'react-native';
...
var ScrollResponderMixin = {
mixins: [Subscribable.Mixin],
componentWillMount: function() {
...
this.addListenerOn(DeviceEventEmitter,
'keyboardWillShow',
this.scrollResponderKeyboardWillShow);
...
},
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.
```js
...
componentWillMount: function() {
DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
// handle event.
});
}
...
```

### 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,
Expand Down

0 comments on commit 98c8d62

Please sign in to comment.