i have CameraModule.kt which is android native module and CameraHomeFragment in a react-native app. And i need to send some data from the fragment to react-native
CameraModule.kt :
class CameraModule(reactContext: ReactApplicationContext): ReactContextBaseJavaModule(reactContext) {
override fun getName(): String = "CameraModule"
private val context = reactApplicationContext
@ReactMethod
fun openCamera(){
val intent = Intent(context,CameraActivity::class.java)
if(intent.resolveActivity(context.packageManager) != null){
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
}
fun emitEvent() {
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("eventName", "params")
}
}
CameraHomeFragment.kt :
class CameraHomeFragment : Fragment() {
private var viewBinding: FragmentCameraHomeBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
viewBinding = FragmentCameraHomeBinding.inflate(inflater, container, false)
return viewBinding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding?.imageCaptureButton?.setOnClickListener {
// emit an event here on clicking button
}
}
}
Have read the docs here, but did not said how to send event from other class where react application context is not available. How to do it?