Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a result from a react native app integrated into an existing android app

I found a lot of articles speaking about how to integrate react native into existing android apps. But none of them explains how to get a result back from the react native code in order to use it directly in Java android source code.

Explanations : I have a react native app that is doing a specific job and displays the result in a popup window. I want to integrate this app (doing the necessary modifications) to be able to get this specific result into my android Java source code instead of displaying it into a react native popup.

I searched all the internet but found nothing about this.

like image 294
Marc Avatar asked Oct 20 '17 11:10

Marc


1 Answers

Maybe I'm not understanding what you're wanting to do.. but can't you just build a native module, and have your React Native app call that function when it's done to pass the data to your native code?

Let's say you have a React Native portion of your app that takes the user's first and last name as input, and they hit submit.

You would code your RN javascript as normal: have 2 TextInput fields with their values mapped to your state, and a Button with a onPress handler. In the onPress handler, you would call your Native Module and pass the parameters

import { 
    NativeModules
} from 'react-native';

const NativeNameSubmission = NativeModules.NameSubmission;

...

    <Button
        onPress={() => NativeNameSubmission.submitName(this.state.firstName, this.state.lastName)}
        title="Submit"/>

Your Native Module would look something like this:

public class RCTNameSubmission extends ReactContextBaseJavaModule {

    public RCTNameSubmission(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "NameSubmission";
    }

    @ReactMethod
    public void submitName(String firstName, String lastName) {
        // save the name data for later use
        ...
        // Exit React App
        getCurrentActivity().finish();
    }
}
like image 179
Jeremy Avatar answered Nov 03 '22 00:11

Jeremy