React Native Android: Showing an Activity from Java

爱⌒轻易说出口 提交于 2020-01-01 09:33:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41698230/react-native-android-showing-an-activity-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!