react-native: Is sharing ReactInstanceManager across multiple activities broken?

I’m trying to share a ReactInstanceManager singleton across multiple activities. I’ve wired my ReactInstanceManager instance the my activities life cycle methods, so when ReactActivity.onDestroy is called, the mReactInstanceManager also gets destroyed and then mCurrentReactContext is set to null.

When opening a new activity and instanciating react views with the singelton manager everything works fine. But when I close the second activity with the back button, it’s onDestroy() is invoked after onResume() of the first activity. Now when I try to interact with react views i get the following warning message:

03-10 13:35:33.164 1627-1627/com.awesomeproject W/unknown:React: Unable to handle touch in JS as the catalyst instance has not been attached

since mReactInstanceManager.getCurrentReactContext() in handleTouchEvent() returns null.

Is sharing ReactInstanceManager across activities/fragment still supported?

I’m running RN 0.20.0 on Android. The repro scenario is very simple: create a react activity (MainActivity). Open another activity from it, and click the back button to go back to the MainActivity. results: any RN view in MainActivity can’t be interacted with.

Logs:

03-10 10:46:10.043 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onCreate
03-10 10:46:10.101 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onStart
03-10 10:46:10.101 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onResume
// Opening second activity
03-10 10:46:48.320 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onPause
03-10 10:46:48.325 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onCreate
03-10 10:46:48.328 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onStart
03-10 10:46:48.328 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onResume
03-10 10:46:48.771 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onStop
// Back button clicked
03-10 10:47:29.772 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onPause
03-10 10:47:29.775 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onStart
03-10 10:47:29.775 10740-10740/com.awesomeproject V/RctActivity[MainActivity]: onResume
03-10 10:47:30.105 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onStop
03-10 10:47:30.105 10740-10740/com.awesomeproject V/RctActivity[SecondActivity]: onDestroy

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Removing the calls to ReactInstanceManager.onDestroy() from any activities will prevent the instance manager from being destroyed.

I have moved the call to ReactInstanceManager.onDestroy to the Application’s onTerminate() method. This approach is working for me so far. Would be great to know if I should be doing it anywhere else.

This occurs because onResume sets the current activity, but onDestroy is called after this (when going back) which then removes the reference.

ReactInstanceManager singleton across multiple activities +1 @mkonicek

@satya164 wonderful, will do.

@guyca Sure. Ping me when you do.