react-native-screens icon indicating copy to clipboard operation
react-native-screens copied to clipboard

[Android] Crash when launching 2 ReactFragment in an activity

Open chenxiangcxc opened this issue 1 year ago • 6 comments

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

  1. Launching 2 ReactFragment one on top of the other, let's say fragment1 at bottom and fragment2 at top.
  2. Navigate back from fragment2.
  3. Click anything on fragment1 to navigate to elsewhere.
  4. 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

chenxiangcxc avatar Apr 15 '25 07:04 chenxiangcxc

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?

github-actions[bot] avatar Apr 15 '25 07:04 github-actions[bot]

@chenxiangcxc can you provide a reproduction of this issue? Otherwise it will be hard to track down what goes on.

kkafar avatar Apr 15 '25 07:04 kkafar

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.

Image Image Image

Steps to reproduce

  1. 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.
  2. Navigate back from fragment2 to fragment 1.
  3. At this time you will see activity reference in the global ReactContext is set null
  4. Crash will occur when computeDummyLayout is called in ScreenDummyLayoutHelper in fragment1, by exception from requireActivity()

chenxiangcxc avatar Apr 16 '25 13:04 chenxiangcxc

+1

estebanGTS avatar Aug 20 '25 15:08 estebanGTS