title: React Native - how can I emit an event to RN from MainActivity.java?
body: <p>In the React Native AppState library:<br>
  <strong>iOS</strong> has three states background->inactive->active<br>
  <strong>Android</strong> only has background->active</p>
  
  <p>When an Android app is fully backgrounded the MainActivity goes from <strong>onPause -> onStop</strong></p>
  
  <p>When there is a System Notification e.g. an In app purchase it goes to <strong>onPause</strong></p>
  
  <p>I need to run some code when the app goes from background to the foreground<br>
  <strong>onStop -> onResume</strong>  </p>
  
  <p>I don't want it to run if the app was briefly paused because of a system notification<br>
  <strong>onPause -> onResume</strong></p>
  
  <p>Is this possible? The lifecycle events for React do not have an onHostStop</p>
  
  <p>I tried emitting an event from MainActivity for each Activity lifecycle event but that caused the App to crash with a null pointer exception.</p>
  
  <p>Is it even possible to emit an event to React Native from MainActivity?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong> Added code to show attempt to emit event from MainActivity</p>
  
  <p><strong>MainActivity.java snippet</strong></p>
  
  <pre><code>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");
      }
  }
  </code></pre>
  
  <p><strong>React Native</strong></p>
  
  <pre><code>const nativeEventListener = DeviceEventEmitter.addListener('onStop',
    (e)=&gt;{
      console.log("NATIVE_EVENT");
      dispatch({type: "NATIVE_EVENT"})
  })
  </code></pre>
  
  <p><strong>error in logcat</strong></p>
  
  <pre><code>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)
  </code></pre>