从集成到现有 Android 应用程序中的 React Native 应用程序获取结果 [英] Get a result from a react native app integrated into an existing android app

查看:33
本文介绍了从集成到现有 Android 应用程序中的 React Native 应用程序获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多文章都在谈论如何将 react native 集成到现有的 android 应用程序中.但是他们都没有解释如何从 react native 代码中获取结果,以便直接在 Java android 源代码中使用它.

I found a lot of articles speaking about how to integrate react native into existing android apps. But none of them explains how to get a result back from the react native code in order to use it directly in Java android source code.

说明:我有一个 React Native 应用程序,它正在执行特定的工作并在弹出窗口中显示结果.我想集成这个应用程序(进行必要的修改),以便能够将这个特定的结果添加到我的 android Java 源代码中,而不是将它显示到本机弹出窗口中.

Explanations : I have a react native app that is doing a specific job and displays the result in a popup window. I want to integrate this app (doing the necessary modifications) to be able to get this specific result into my android Java source code instead of displaying it into a react native popup.

我搜索了所有互联网,但一无所获.

I searched all the internet but found nothing about this.

推荐答案

也许我不明白你想要做什么..但是你不能只构建一个原生模块,然后让你的 React Native 应用程序调用完成将数据传递给您的本机代码后的那个函数?

Maybe I'm not understanding what you're wanting to do.. but can't you just build a native module, and have your React Native app call that function when it's done to pass the data to your native code?

假设您的应用有一个 React Native 部分,它将用户的名字和姓氏作为输入,然后他们点击提交.

Let's say you have a React Native portion of your app that takes the user's first and last name as input, and they hit submit.

您可以像往常一样编写您的 RN javascript:有 2 个 TextInput 字段,它们的值映射到您的状态,以及一个带有 onPress 处理程序的 Button.在 onPress 处理程序中,您将调用您的本机模块并传递参数

You would code your RN javascript as normal: have 2 TextInput fields with their values mapped to your state, and a Button with a onPress handler. In the onPress handler, you would call your Native Module and pass the parameters

import { 
    NativeModules
} from 'react-native';

const NativeNameSubmission = NativeModules.NameSubmission;

...

    <Button
        onPress={() => NativeNameSubmission.submitName(this.state.firstName, this.state.lastName)}
        title="Submit"/>

你的原生模块看起来像这样:

Your Native Module would look something like this:

public class RCTNameSubmission extends ReactContextBaseJavaModule {

    public RCTNameSubmission(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    @ReactMethod
    public void submitName(String firstName, String lastName) {
        // save the name data for later use
        ...
        // Exit React App
        getCurrentActivity().finish();
    }
}

这篇关于从集成到现有 Android 应用程序中的 React Native 应用程序获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆