[Android] Crash when launching 2 ReactFragment in an activity
Description
My app has an activity + several fragments design for Android phone, and integrate react native into fragment. When launching 2 ReactFragment one on top of the other, the app will be crash. Below is the stack trace:
JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception java.lang.IllegalArgumentException: [RNScreens] Attempt to use context detached from activity
java_vm_ext.cc:599] at android.app.Activity com.swmansion.rnscreens.utils.ScreenDummyLayoutHelper.requireActivity() (ScreenDummyLayoutHelper.kt:223)
java_vm_ext.cc:599] at float com.swmansion.rnscreens.utils.ScreenDummyLayoutHelper.computeDummyLayout(int, boolean) (ScreenDummyLayoutHelper.kt:181)
java_vm_ext.cc:599] at void com.facebook.jni.NativeRunnable.run() (NativeRunnable.java:-2)
java_vm_ext.cc:599] at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:959)
java_vm_ext.cc:599] at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:100)
java_vm_ext.cc:599] at void com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(android.os.Message) (MessageQueueThreadHandler.kt:20)
java_vm_ext.cc:599] at boolean android.os.Looper.loopOnce(android.os.Looper, long, int) (Looper.java:232)
java_vm_ext.cc:599] at void android.os.Looper.loop() (Looper.java:317)
java_vm_ext.cc:599] at void com.facebook.react.bridge.queue.MessageQueueThreadImpl$Companion.startNewBackgroundThread$lambda$1(com.facebook.react.common.futures.SimpleSettableFuture) (MessageQueueThreadImpl.kt:175)
java_vm_ext.cc:599] at void com.facebook.react.bridge.queue.MessageQueueThreadImpl$Companion.$r8$lambda$ldnZnqelhYFctGaUKkOKYj5rxo4(com.facebook.react.common.futures.SimpleSettableFuture) (MessageQueueThreadImpl.kt:-1)
java_vm_ext.cc:599] at void com.facebook.react.bridge.queue.MessageQueueThreadImpl$Companion$$ExternalSyntheticLambda0.run() (D8$$SyntheticClass:0)
java_vm_ext.cc:599] at void java.lang.Thread.run() (Thread.java:1012)
java_vm_ext.cc:599]
java_vm_ext.cc:599] in call to NewStringUTF
java_vm_ext.cc:599] from void com.facebook.jni.NativeRunnable.run()
runtime.cc:708] Runtime aborting...
runtime.cc:708] Dumping all threads without mutator lock held
If a native fragment on top of ReactFragment, it'll be fine.
Steps to reproduce
- Launching 2 ReactFragment one on top of the other, let's say fragment1 at bottom and fragment2 at top.
- Navigate back from fragment2.
- Click anything on fragment1 to navigate to elsewhere.
- Crash will occur.
Snack or a link to a repository
N/A
Screens version
4.10.0
React Native version
0.79.0
Platforms
Android
JavaScript runtime
None
Workflow
None
Architecture
None
Build type
None
Device
None
Device model
No response
Acknowledgements
Yes
Hey! 👋
The issue doesn't seem to contain a minimal reproduction.
Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?
@chenxiangcxc can you provide a reproduction of this issue? Otherwise it will be hard to track down what goes on.
Hey @kkafar I've debugged for a while, seems activity reference in the global ReactContext is set null when the top ReactFragment is pop, then the bottom ReactFragment couldn't get the activity reference when calling computeDummyLayout in react native screens.
Steps to reproduce
- In a project which has 1 activity + multi fragments, Launching 2 ReactFragments one on top of the other, let's say fragment1 at bottom and fragment2 at top.
- Navigate back from fragment2 to fragment 1.
- At this time you will see activity reference in the global ReactContext is set null
- Crash will occur when computeDummyLayout is called in ScreenDummyLayoutHelper in fragment1, by exception from
requireActivity()
+1