React Native - 如何从MainActivity.java?

时间:2016-11-30 21:33:17

标签: android react-native

在React Native AppState库中:
iOS 有三种状态background-> inactive-> active
Android 只有background->有效

当Android应用完全落后时,MainActivity来自 onPause - >的onStop

当存在系统通知时,例如在应用内购买中,它会转到 onPause

当应用从背景到前景时,我需要运行一些代码 onStop - > onResume

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

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

我尝试从每个Activity生命周期事件的MainActivity发出一个事件,但是这导致App崩溃并出现空指针异常。

甚至可以从MainActivity向React Native发出事件吗?

由于

编辑添加了代码以显示尝试从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");
    }
}

反应原生

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)

1 个答案:

答案 0 :(得分:16)

尝试更新您的MainActivity.java:

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()来访问上下文,但是如果你可以获得ReactContext它似乎可以工作来自ReactInstanceManager