0

I followed the tutorial on this site: https://facebook.github.io/react-native/docs/embedded-app-android.html

Now I want to have a function in my javascript code like this:

greetingFunction(){

     return "HELLO, WORLD";
}

I want to pass the string "HELLO, WORLD" to my java android code and set the screen to display that string.

How can I do this?

1
  • want to do exactly reverse..want to pass data from java android to javascript.. Commented Jun 12, 2018 at 6:43

1 Answer 1

2

You should create a custom module to call custom native methods in Java where you can pass the data you need.

For instance if your custom native module implements the following method:

@ReactMethod
public void myMethod(String message) {
  // Here we show a toast message
  Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

Then you can call that method from JavaScript:

NativeModules.MyCustomModule.myMethod("HELLO WORLD");

Be sure to properly follow all steps described in the doc above to properly register your custom module.

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

2 Comments

I followed the instructions in the doc, and when i run the app it says it cant find the module "./ToastAndroid", which is what i returned in getName(). How many javascript files did you have after you did this? 2? I want to see if i did this correctly And also, that doc tells you how to pass data/call java function in javascript, but I also want to be able to pass data back from the javascript to the java android. How would one accomplish such a task?
Did you properly register your module: facebook.github.io/react-native/docs/… ?

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.