Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android: Showing an Activity from Java

I need to show an Activity ( Native android, Java ) in React-native. I know it have been asked few times, But none helped me. I didn't find any tutorial or documentations on how to call/open the activity in React-Native. where to put the activity and how to register/add it to project.

Is there any tutorial or sample code?

I'm using react-native-camera , when i run it from RN, it shows a view from rn-camera, i looked into it's source code but it doesn't have an Activity.

If you could tell which modules for react-native are using activities it could help as well. (showing an android activity in react native).

There is some documentations on how to add react native to existing android projects but i couldn't find any guide on how to import an activity from android.

I'd really appreciate your help.

like image 556
Ata Mohammadi Avatar asked Jan 17 '17 13:01

Ata Mohammadi


People also ask

Can Java be used in React Native?

React Native is a framework that allows you to build native mobile apps using JavaScript. Normally, you'd need to program your mobile app using Java (for Android) and Swift/Obj-C (for iOS).

Where is MainActivity Java in React Native?

This file is located in android/app/src/main/java/com/<yourproject>/MainActivity. java .

What is Turbomodule?

TurboModules. The TurboModules system is an enhancement of Native Modules. In their current architecture, a table holds the module registry, and when the JavaScript code calls a specific native module, the indices of the module and the methods are passed to Java/ObjC, which invoke the specific methods.


1 Answers

Hope I understand your issue correctly: What you want is to show an Activity (java code) from javascript code.

I would suggest to implement a native module: https://facebook.github.io/react-native/docs/native-modules-android.html

Native module is a bridge between java and javascript. So if your native module has this:

@Override
public String getName() {
    return "YourModule";
}

@ReactMethod
public void showYourActivity() {
   Intent intent = new Intent(mContext, YourActivity.class); // mContext got from your overriden constructor
   getCurrentActivity().startActivity(intent);
}

then in your js code:

import {NativeModules} from 'react-native';

NativeModule.YourModule.showYourActivity();

hope that helps. You also can transfer data between them as well, please check at the document.

like image 119
idealweek.net Avatar answered Oct 19 '22 23:10

idealweek.net