React Native - 如何从 MainActivity.java 向 RN 发出事件? [英] React Native - how can I emit an event to RN from MainActivity.java?

查看:61
本文介绍了React Native - 如何从 MainActivity.java 向 RN 发出事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 React Native AppState 库中:
iOS 具有三种状态 background->inactive->active
Android 只有背景->活动

In the React Native AppState library:
iOS has three states background->inactive->active
Android only has background->active

当 Android 应用完全后台运行时,MainActivity 从 onPause -> onStop

When an Android app is fully backgrounded the MainActivity goes from onPause -> onStop

当有系统通知时,例如应用内购买会转到 onPause

When there is a System Notification e.g. an In app purchase it goes to onPause

当应用程序从后台转到前台时,我需要运行一些代码
onStop -> onResume

I need to run some code when the app goes from background to the foreground
onStop -> onResume

如果应用程序因系统通知而短暂暂停,我不希望它运行
onPause -> onResume

I don't want it to run if the app was briefly paused because of a system notification
onPause -> onResume

这可能吗?React 的生命周期事件没有 onHostStop

Is this possible? The lifecycle events for React do not have an onHostStop

我尝试从 MainActivity 为每个 Activity 生命周期事件发出一个事件,但这导致应用程序因空指针异常而崩溃.

I tried emitting an event from MainActivity for each Activity lifecycle event but that caused the App to crash with a null pointer exception.

是否可以从 MainActivity 向 React Native 发出事件?

Is it even possible to emit an event to React Native from MainActivity?

谢谢

EDIT 添加代码以显示尝试从 MainActivity 发出事件

EDIT Added code to show attempt to emit event from MainActivity

MainActivity.java 片段

import com.facebook.react.ReactActivity;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class MainActivity extends ReactActivity {

    DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter;

    @Override
    public void onStop() {
        super.onStop();
        eventEmitter.emit("onStop", "ActivityonStop");
    }
}

React Native

const nativeEventListener = DeviceEventEmitter.addListener('onStop',
  (e)=>{
    console.log("NATIVE_EVENT");
    dispatch({type: "NATIVE_EVENT"})
})

logcat 中的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.realer.android, PID: 15251
java.lang.RuntimeException: Unable to stop activity {com.realer.android/com.realer.android.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter.emit(java.lang.String, java.lang.Object)' on a null object reference
   at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3837)
   at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3886)
   at android.app.ActivityThread.-wrap25(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6077)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter.emit(java.lang.String, java.lang.Object)' on a null object reference
   at com.realer.android.MainActivity.onStop(MainActivity.java:40)
   at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1289)
   at android.app.Activity.performStop(Activity.java:6839)
   at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3834)
   at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3886) 
   at android.app.ActivityThread.-wrap25(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:154) 
   at android.app.ActivityThread.main(ActivityThread.java:6077) 
   at java.lang.reflect.Method.invoke(Native Method)

推荐答案

尝试像这样更新 MainActivity.java:

Try updating your MainActivity.java like this:

public class MainActivity extends ReactActivity {

    @Override
    public void onStop() {
        super.onStop();

        WritableMap params = Arguments.createMap(); // add here the data you want to send
        params.putString("event", "ActivityonStop"); // <- example

        getReactInstanceManager().getCurrentReactContext()
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("onStop", params);
    }
}

让我知道这是否有效.在我的应用程序中,我通常从扩展 ReactContextBaseJavaModule 的类发送事件,在那里我可以通过调用 getReactApplicationContext() 访问上下文,但如果你可以从ReactInstanceManager中获取ReactContext.

Let me know if this works. In my apps I usually send the events from a class that extends ReactContextBaseJavaModule where I can access the context just by calling getReactApplicationContext(), but it seems that it might work if you can obtain the ReactContext from the ReactInstanceManager.

这篇关于React Native - 如何从 MainActivity.java 向 RN 发出事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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