Call native android activity from react-native

Call native activity methods from react-native

To call native activity from react-native, first I am considering that our react-native project has successfully integrated into existing Android studio project if not then read my previous articles on

How to setup react-native step by step Click here

How to integrate react-native in existing android app Click here

Before moving forward I consider that you complete react-native setup and integration in existing native android app, and now to need to call the native android method from react-native to do this

We have use react-native

Native Modules

Create native module

This guide will use one method, let’s say we would like to call this method from JavaScript

Create a call OpenAndroid.java and extends this call with ReactContextBaseJavaModule

ReactContextBaseJavaModule  Require the method call getName the purpose of this method is to return string name in javaScript, you can access method by calling React.NativeModules.NativeCalls

We need to annotate the method with @ReactMethod to expose this method in JavaScript, the return type of this method is always Void and the React-native bridge is asynchronous

We can pass following argument type from native module

Register the Module

We have to register this module in java code, if module not registered then it will not available in JavaScript to do this create one class name OpenAndroidPackage.java and implement it with React-Package as follows

Now, our native module created and the package is registered,

we need to pass this registered package to react-native project through the getPackage method of the MainApplication.java

you will get this file at the following path android/app/src/main/java/com/yourAppName/MainApplication.java

Or if you are open react-native project from some other activity like below then you want to pass our package like below

Use native module in JavaScript

To access native module in JavaScript, we need to wrap native module with javaScript module to do this

create JS module file in the react-native project,  ex. create NativeCalls.js and add the following line in it

Then import this NativeCalls module where you want to use like as below

and call the method like

That set, you can use any existing android module using this method, just create module package–> Register that module package –> pass this package through the .addPackage method and done

 

Thank you 🙂 enjoy coding

34 Replies to “Call native android activity from react-native”

  1. Excellent web site. A lot of helpful information here. I am sending it to some friends ans also sharing in delicious. And certainly, thank you for your effort!|

Leave a Reply

Your email address will not be published. Required fields are marked *