Communication between native and React Native components in Android is a crucial aspect of app development. React Native is undoubtedly a powerful tool for creating applications on both Android and iOS platforms. However, despite its comprehensiveness, there are instances where certain components must be developed natively to gain more granular control over their behavior.
In such scenarios, when we blend native and React Native components within the same application, the need for seamless communication between these two worlds arises. Ensuring a smooth interaction between these distinct parts becomes essential to achieve a cohesive and fully functional application. By establishing effective communication channels, developers can leverage the strengths of both approaches and deliver a superior user experience.
Communication from android to React-native:
Suppose we have a back button placed on the action bar of an Android application, and there’s a specific action that needs to be executed in the React Native codebase upon clicking this button. To achieve this seamless interaction, a communication bridge is essential.
The communication bridge serves as a crucial link between the native Android code and the React Native components. It facilitates the exchange of information and events between these two distinct parts of the application. When the back button is clicked, the bridge enables the transmission of this event to the React Native environment, triggering the necessary action or function. This way, the application can efficiently coordinate actions and responses across the native and React Native realms, ensuring a smooth and cohesive user experience. Let’s create a bridge between the native Android code and React Native components by following these steps:
Step 1:
- Create one java class and extend it from ReactContextBaseJavaModule
- Write one constructor
- override getName method
public class AndroidReactNativeBridgeModule extends ReactContextBaseJavaModule implements LifecycleEventListener{
private static ReactApplicationContext reactApplicationContext;
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener();
AndroidReactNativeBridgeModule(ReactApplicationContext reactContext) {
super(reactContext);
reactApplicationContext = reactContext;
reactApplicationContext.addLifecycleEventListener(this);
reactApplicationContext.addActivityEventListener(mActivityEventListener);
}
@Override
public void onHostResume() {
// Activity `onResume`
}
@Override
public void onHostPause() {
// Activity `onPause`
}
@Override
public void onHostDestroy() {
// Activity `onDestroy`
}
@Override
public String getName() {
return "AndroidReactNativeBridgeModule";
}
@ReactMethod
public String getSDKName() {
System.out.println("Greetings from Java");
return "AndroidReactNativeBridgeModule";
}
Step 2:
Create a new module file and extend from ReactPackage, override createNativeModules and add modules created in the previous step in the array list.
public class AndroidReactNativeBridgePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new AndroidReactNativeBridgeModule(reactContext));
return modules;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
Step 3:
Go to Application class and add package created in previous step, in getPackage method
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(AndroidReactSDKManager.getInstance().getReactPackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
Step 4:
In the ReactNative project, open index.js file and import module.
Import { NativeModules } from ‘react-native’
Module.exports = NativeModules.AndroidReactNativeBridgeModule;
Step 5:
In the android file write this to trigger the event in the back button natively (android)
WritableMap params = Arguments.createMap();
params.putString(“eventProperty”,”someValue”);
ReactApplication rnApp = (ReactApplication) getApplicationContext();
rnApp.getReactNativeHost().getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RXTDeviceEmitter.class).emit(“BackActionTrigger”,Arguments.createMap());
Step 6:
In the react native component , we can add this code as mentioned below.
If (Platform.OS == ‘android’)
{
Const eventEmitter = new NativeEventEmitter(NativeModules.AndroidReactNativeBridge)
eventEmitter.addListener(‘BackActionTrigger’,(event) => {
console.log(‘event.eventProperty’,event.eventProperty)
})
}
Reference: https://reactnative.dev/docs/communication-android
Conclusion:
Additionally, this seamless communication between native and React Native components in Android allows developers to create highly interactive and dynamic applications, where both technologies complement each other flawlessly. By breaking down the barriers and leveraging the communication bridge effectively, the development process becomes more efficient, leading to faster iterations and improved collaboration among the development team.
Furthermore, as mobile app development evolves, having expertise in both native and React Native environments and understanding their communication intricacies becomes a valuable asset. Developers who can navigate and optimize this communication bridge effectively can build versatile applications that cater to a broader audience, ensuring a competitive edge in the ever-evolving app market. Embracing this cohesive approach not only streamlines development but also paves the way for innovative and engaging user experiences.