In the React Native documentation for integrating with Android, it includes this snippet for integrating with Android:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModulePath("index")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
setContentView(mReactRootView);
}
However, when I use this as-is I get a 100% repro memory leak caused by a ThemedReactContext
holding a reference to the ReactRootView
which holds a reference to my custom activity.
This is because the Context
argument passed to the constructor of ReactRootView
is this
, which is a reference to my custom activity.
Instead, if I do:
mReactRootView = new ReactRootView(getApplication());
I get no memory leaks.
Is it safe to change the source of my context for a new ReactRootView, and is this a bug that should either a) be fixed or b) should see the documentation changed?
It looks like the context is only used to initialize the FrameLayout
. Technically passing in an application context will work to avoid the memory leak, however the styling might be messed up since "the inflation will be done with the default theme for the system in which you are running, not what's defined in your application."
See this article on different context capabilities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With