Reputation: 2096
Currently the following code does successfully get the text that is being shared to the app, but I am having trouble finding out how to send this event to the Javascript side. I tried creating a new NavigationReactGateway
but my app just crashes. I'm new to Java so I don't even know if I did it properly.
public class MainActivity extends SplashActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
protected String getMainComponentName() {
return "MyApplication";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Send event to Javascript as "share" event
}
}
}
What is the correct way to send this event to Javascript?
The app has react-native-navigation
and there are a lot of references to an event emitter, I'm just unsure how to get the react context to emit the event. If that makes any sense.
Upvotes: 2
Views: 2239
Reputation: 2096
I was able to modify the handleSendText
method to the following to send an event.
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
ReactContext context = NavigationApplication.instance.getReactGateway().getReactContext();
if (context != null) {
WritableMap params = Arguments.createMap();
params.putString("event", sharedText);
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("share", params);
}
}
}
I also had to change from using the onCreate
method to using onStop
, then the react context has been initialized and my event launches whether the app was already open or not.
Upvotes: 2
Reputation: 18538
You can use webView.addJavascriptInterface()
You can find documentation here.
Upvotes: 0