question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] Shared ReactInstanceManager in a singleton between multiple activities failing while showing a modal

See original GitHub issue

I integrated a React Native app into an existing Android app. When a native app requests a React Native view, I start a new activity with a React Native view in it. I wrapped ReactInstanceManager in a singleton class and initialize it when the Android app loads.

The problem I experience is that ReactInstanceManager cannot be shared between multiple instances of ReactNative views. For example, if I start one React Native activity and show a share sheet using the Share component, the next time I start a new activity and provide it the cached ReactInstanceManager, it will fail while trying to show a Modal. When the React Native tries to call mDialog.show it fails with android.view.WindowManager$BadTokenException: Unable to add window. It looks like it tries to attach a dialog to a non-existent activity. It has something to do with the React Themed Context I guess.

It looks like ReactInstanceManager cannot be loaded once and shared safely between multiple instances of React Native views.

If anyone can share insight on how to share ReactInstanceManager properly, I would really appreciate that. Currently I am refreshing the manager every time I close a React Native Activity, but I would like to have a single instance.

  1. react-native -v: 0.42.3
  2. node -v: v6.11.0
  3. npm -v: 5.2.0
  4. platform: Android

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ManishPatiyalcommented, Jul 27, 2017

I also have the same requirement and need to have singleton pattern for ReactInstanceManager as I need to send custom events using RCTDeviceEventEmitter to react. For this I need the ReactInstanceManager which is null at the time I build it on android activities oncreate() method .

1reaction
KingAlencommented, Apr 19, 2018

+1 In https://facebook.github.io/react-native/docs/integration-with-existing-apps.html It says: A ReactInstanceManager can be shared by multiple activities and/or fragments. You will want to make your own ReactFragment or ReactActivity and have a singleton holder that holds a ReactInstanceManager. When you need the ReactInstanceManager (e.g., to hook up the ReactInstanceManager to the lifecycle of those Activities or Fragments) use the one provided by the singleton. But no example

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sharing a single ReactInstanceManager between multiple ...
The problem is that it looks like sharing a ReactInstanceManager between multiple activities cause some problems.
Read more >
Singletons in Android. Avoid them or at least think twice…
In fact there are many articles about how bad Singleton Pattern ... In fact, you can choose only between activities, views and fragments....
Read more >
Integration with Existing Apps · React Native - API Manual
React Native is great when you are starting a new mobile app from scratch. However, it also works well for adding a single...
Read more >
Managing a Shared Resource Using a Singleton
You create simple singletons using a static type property, which is guaranteed to be lazily initialized only once, even when accessed across multiple...
Read more >
Singletons And Shared Instances - Alain Schlesser
And although my personal experience has shown time and time again that ... No tight coupling between any of the objects, all objects...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found