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

[Android] ViewGroup.dispatchAttachedToWindow crash with Fabric architecture

Open fjmorant opened this issue 6 months ago β€’ 7 comments

Description

Experiencing NullPointerException crashes in ViewGroup.dispatchAttachedToWindow when using React Native Screens with the new architecture (Fabric) enabled.

Stack Trace

      Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3526)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3525)
   at android.view.ViewGroup.addViewInner(ViewGroup.java:5330)
   at android.view.ViewGroup.addView(ViewGroup.java:5116)
   at android.view.ViewGroup.addView(ViewGroup.java:5056)
   at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:881)
   at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:565)
   at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
   at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
   at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
   at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1751)
   at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:323)
   at com.swmansion.rnscreens.ScreenStack.onUpdate(ScreenStack.kt:262)
   at com.swmansion.rnscreens.ScreenContainer.performUpdates(ScreenContainer.kt:359)
   at com.swmansion.rnscreens.ScreenContainer.onScreenChanged$lambda$7(ScreenContainer.kt:342)
   at android.os.Handler.handleCallback(Handler.java:991)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.kt:20)
   at android.os.Looper.loopOnce(Looper.java:232)
   at android.os.Looper.loop(Looper.java:317)
   at android.app.ActivityThread.main(ActivityThread.java:8934)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:591)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
        

Environment

  • React Native version: 0.79.3
  • React Native Screens version: 4.11.1
  • Platform: Android
  • Architecture: Fabric (New Architecture) enabled
  • Device: Multiple Android devices (not device-specific)

Steps to Reproduce

  1. Enable Fabric architecture in your React Native app
  2. Use React Native Screens for navigation
  3. Perform rapid navigation between screens
  4. The crash occurs randomly during fragment transactions

Expected Behavior

Navigation should work smoothly without crashes.

Actual Behavior

App crashes with NullPointerException in ViewGroup.dispatchAttachedToWindow.

Additional Context

  • This started happening after enabling the new architecture (Fabric)
  • The crash is intermittent and seems to be related to race conditions
  • Occurs during fragment transactions when views are being attached/detached
  • Not reproducible consistently, but happens frequently enough to be a critical issue

Possible Root Cause

The issue appears to be a race condition between:

  1. React Native's Fabric architecture view management
  2. Android's fragment transaction system
  3. React Native Screens' fragment-based navigation

When Fabric nullifies views during its async operations, the fragment transaction tries to access those views, causing the NullPointerException.

Steps to reproduce

  1. Enable Fabric architecture in your React Native app
  2. Use React Native Screens for navigation
  3. Perform rapid navigation between screens
  4. The crash occurs randomly during fragment transactions

Snack or a link to a repository

cannot share

Screens version

4.11.1

React Native version

0.79.3

Platforms

Android

JavaScript runtime

Hermes

Workflow

React Native (without Expo)

Architecture

Fabric (New Architecture)

Build type

Release mode

Device

Real device

Device model

No response

Acknowledgements

Yes

fjmorant avatar Jul 02 '25 10:07 fjmorant

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 Jul 02 '25 10:07 github-actions[bot]

I believe having this in fun onUpdate, it will stop the crash:

  // HOTFIX: Simple exception handling to prevent ViewGroup.dispatchAttachedToWindow crash
  try {
      transaction.commitNowAllowingStateLoss()
  } catch (e: Exception) {
      android.util.Log.w(TAG, "Fragment transaction failed: ${e.message}")
      // App continues without crashing
  }

Only thing is I am not sure the possible side effects of this

fjmorant avatar Jul 02 '25 12:07 fjmorant

Temporary fix that worked for me:

Switched from @react-navigation/native-stack to @react-navigation/stack.

Why this helps:

native-stack uses react-native-screens to control real native view hierarchies (UINavigationController on iOS / FragmentManager on Android).

This adds complexity because the native view hierarchy must stay perfectly in sync with the React tree.

In some edge cases (e.g., very fast navigation changes, async dispatches, race conditions during unmount), React state and native state can go out of sync β€” leading to crashes like dispatchCancelPendingInputEvents, null pointer exceptions, or unexpected detach errors.

The JS-based @react-navigation/stack keeps the entire stack in the JS realm, rendering screens as React components inside a single native container view.

This removes the native view detach/attach lifecycle and avoids those native crashes β€” at the cost of slightly less performant transitions.

⚠️ Note:

This should be treated as a temporary workaround.

Monitor upstream fixes, or debug what triggers the state mismatch so you can safely switch back to native-stack for better native performance once stable.

Hope this helps anyone facing the same issue! πŸš€

vishavjeet1999 avatar Jul 14 '25 13:07 vishavjeet1999

We’re also experiencing the same issue since we updated to React Native 0.79 with the new architecture and to react-native-screens version 4.14.1.

Reproducing the issue is quite difficult, as we only have reports from Sentry (around 200 events in the past 12 days).

Switching to the JS-based @react-navigation/stack is not an option due to performance considerations.

mcastets avatar Nov 07 '25 13:11 mcastets

Any update?

ScreenStack.onUpdate java.lang.NullPointerException - Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference

Facing the same issue

saiyam-zepto-dev avatar Nov 24 '25 14:11 saiyam-zepto-dev

We have the same problem on React Native 81.5 and react-native-screens version 4.16.0

I'm unable to replicate the issue but getting hundreds of events on Sentry.

ericpoulinnz avatar Dec 07 '25 20:12 ericpoulinnz

I am getting this on Paper. Only seems to affect Android 8 and 8.1. Started when I upgraded to Expo 54/RN 0.81, which bumped the RNS version from 4.11.0 to 4.16.0

mozzius avatar Dec 10 '25 15:12 mozzius