This repository consists the code of the example app - WealthApp in which the Mobile SDK of Volt is integrated.
- Create a Kotlin class inside the Android folder and extend the
ReactContextBaseJavaModuleinterface.
Override the methodgetNameand enter the name you want to give to this module.
Note: The name you give inside getName will be the name you will be accessing the file in JS module.
override fun getName(): String {
return "VoltSDK"
}
- In the same class, create another method with a prefix
@ReactMethod. The methods only with the prefix can be accessed while bridging and rest of the methods are just regular methods.
@ReactMethod
fun initializeVoltApplication(primaryColor: String, secondaryColor: String) {
val intent = Intent(context, Volt::class.java)
intent.putExtra(Constants.INIT_VOLT,true)
intent.putExtra(Constants.PRIMARY_COLOR, primaryColor)
intent.putExtra(Constants.SECONDARY_COLOR, secondaryColor)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
}
Create an Empty Activity Volt and call this activity with an Intent inside the React Method.
- Create another class which'll be the main class for bridging which extends
ReactPackage.
Override the new methodscreateNativeModulesandcreateViewManagers.
Inside thecreateNativeModulescreate an empty Mutable List and add the VoltSDK class inside it.
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> {
val modules: MutableList<NativeModule> = ArrayList()
modules.add(VoltSDK(reactContext))
return modules.toMutableList()
}
Inside the createViewManagers, return Collections.emptyList()
override fun createViewManagers(reactContext: ReactApplicationContext): MutableList<ViewManager<View, ReactShadowNode<*>>> {
return Collections.emptyList()
}
- Finally, in the
MainApplicationclass of Android inside thegetPackagesmethod, uncomment the code and add the package ofVoltManager.
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new VoltManager());
return packages;
}
- We're all set and the last step is to call the package and method inside our JS module to access the methods in Native Module.
ImportNativeModulesfrom React-Native.
Create a const variable and call your class inside Native Module
const voltSDK = NativeModules.VoltSDK
Finally, access the methods inside voltSDK like so,
const openVoltSDK = () => {
return voltSDK.initializeVoltApplication(primaryColor, secondaryColor)
}