9

How to get this work, in the simplest way, I have trouble getting the callback send to react-native, probably I'm missing something.

@ReactMethod
public void testCallback(Callback cb) {

String sampleText = "Java is fun";
int textLength = sampleText.length();
    try{
        cb.invoke(textLength);
    }catch (Exception e){
        cb.invoke("err");
    }
}

On react-native side

var getNativeCallback = require('react-native-native-callback');


getNativeCallback.testCallback(function (result){
    console.log(result)
})
4
  • What error are you getting ? Commented Oct 25, 2015 at 14:58
  • I already solved it, just need to bridge with reactContenx by starting the reactContenx .startActivity, but I haven't post my own solution yet Commented Oct 25, 2015 at 23:25
  • Could you please post your solution to this? Commented Mar 16, 2016 at 17:59
  • github.com/amalChandran/ReactNative_Android_integration for examples related to callbacks. Commented Apr 4, 2017 at 9:23

1 Answer 1

4

I have to face the same problem, and finally I have to take a different approach, as apparently, there is no Callback type accepted for @ReactProp.

Instead I used the 'Events' way, in order to have response communication from Android native to React Native.

In the Android side I set up a SendEvent function conveniently fired as it needs:

private void sendEventToReactFromAndroid(ReactContext reactContext,String eventName, @Nullable WritableMap params) {
    reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}

@Override
public void customAndroidListenerFunction() {
    sendEvent(mcontext, "EventName", null);

}

Then in the React side, you will expect this event, and parameters if you may like:

var {DeviceEventEmitter} = require('react-native');
...
componentWillMount: function() {
    DeviceEventEmitter.addListener('EventName', function(e: Event) {
        console.log("Received event loud and clear in the React Native side!");
    });
},
...

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

hi i have few doubts here. can we talk here ? I am having problem with this. I want to send some parameters to android native and get some object and parameter too at the same time .
Hi, have you checked the official RN page Native Modules Android ? Specifically that Android React Native callbacks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.