Skip to content

fixes: out if index exception when adding feature view#5795

Merged
salah-ghanim merged 2 commits intoreact-native-maps:masterfrom
CptFabulouso:viewOutOfBounds
Nov 30, 2025
Merged

fixes: out if index exception when adding feature view#5795
salah-ghanim merged 2 commits intoreact-native-maps:masterfrom
CptFabulouso:viewOutOfBounds

Conversation

@CptFabulouso
Copy link
Contributor

Does any other open PR do the same thing?

#5770 possibly tries to address similar issue, but fails. Although my primary focus was to fix #5781

What issue is this PR fixing?

#5781
Possibly also this #5750

How did you test this PR?

I have very similar setup to what the issues above specify, meaning problems after navigating away from the screen with map and navigating back. These changes seem to fix it for my use case.

PR explanation

When navigatin away from the screen, onDetachedFromWindow lifecycle is called, which calls features.clear().
After navigating back to the screen, RN re-mounts the views (I think), and addView methods are called (before onAttachedToWindow).
The addView may be called first with view at index 1, which ends up with IndexOutOfBoundsException, because features ware cleared and we must first add item at index 0.
Only after views are added the onAttachedToWindow is called.

So the fix checks whether the component is detached (paused) and if so, saves the component into savedFeatures instead and after the onAttachedToWindow is called, they get mounted via the existing code there

That's also probably the reason why #5781 (comment) works, because I assume that prevents running the onDetachedFromWindow lifecycle.

@CptFabulouso
Copy link
Contributor Author

I've checked that this resolves #5750 with the provided reproduction repo

@maXXCZ1
Copy link

maXXCZ1 commented Nov 4, 2025

I've checked that this resolves #5750 with the provided reproduction repo

@salah-ghanim I experienced same issue as described here:
#5781 (comment)

This PR fixes the issue for me.

@maXXCZ1
Copy link

maXXCZ1 commented Nov 6, 2025

I've checked that this resolves #5750 with the provided reproduction repo

@salah-ghanim I experienced same issue as described here: #5781 (comment)

This PR fixes the issue for me.

I've checked that this resolves #5750 with the provided reproduction repo

@salah-ghanim I experienced same issue as described here: #5781 (comment)

This PR fixes the issue for me.

I am however still experiencing issue with android map markers being rendered in place of original markers.
This issue:
#5778

@CptFabulouso
Did you experience this? I am on 1.26.18 with this commit as patch.

//edit I am using clustering, but it even if i disable it. Once i try to re-render markers from another screen, there are basic marker Android icons instead of Those that were detached (filtered out) and they stay visible no matter what (duplicate my markers if i reset filters).

@CptFabulouso
Copy link
Contributor Author

@maXXCZ1 No not in my use case. I'd like to look into it, but not sure if I'll find time for that :/
Have you tried turning off detaching inactive screens (I guess it's react-navigation prop?), like mentioned in this issue here #5781 (comment)?

@maXXCZ1
Copy link

maXXCZ1 commented Nov 7, 2025

@CptFabulouso No, i tried but sadly no imprevements. But bug addressed with your PR is in my case solved, thanks.

@code-strategist
Copy link

this crash happened once on prod with this changes. Not sure how to reproduce it.

Exception com.facebook.jni.CppException:
  at com.rnmaps.maps.MapView.safeAddFeature (MapView.java:1162)
  at com.rnmaps.maps.MapView.addFeature (MapView.java:1178)
  at com.rnmaps.fabric.MapViewManager.addView (MapViewManager.java:320)
  at com.rnmaps.fabric.MapViewManager.addView (MapViewManager.java:43)
  at com.facebook.react.fabric.mounting.SurfaceMountingManager.addViewAt (SurfaceMountingManager.java:407)
  at com.facebook.react.fabric.mounting.mountitems.IntBufferBatchMountItem.execute (IntBufferBatchMountItem.kt:122)
  at com.facebook.react.fabric.mounting.MountItemDispatcher.executeOrEnqueue (MountItemDispatcher.kt:325)
  at com.facebook.react.fabric.mounting.MountItemDispatcher.dispatchMountItems (MountItemDispatcher.kt:231)
  at com.facebook.react.fabric.mounting.MountItemDispatcher.tryDispatchMountItems (MountItemDispatcher.kt:89)
  at com.facebook.react.fabric.FabricUIManager$3.runGuarded (FabricUIManager.java:943)
  at com.facebook.react.bridge.GuardedRunnable.run (GuardedRunnable.kt:19)
  at com.facebook.react.fabric.FabricUIManager.scheduleMountItem (FabricUIManager.java:946)
  at com.swmansion.reanimated.NativeProxy.performOperations (NativeProxy.java)
  at com.swmansion.reanimated.NodesManager.performOperations (NodesManager.java:121)
  at com.swmansion.reanimated.NativeProxy.maybeFlushUIUpdatesQueue (NativeProxy.java:491)
  at com.swmansion.worklets.AndroidUIScheduler.triggerUI (AndroidUIScheduler.java)
  at com.swmansion.worklets.AndroidUIScheduler.lambda$new$0 (AndroidUIScheduler.java:27)
  at com.swmansion.worklets.AndroidUIScheduler$1.runGuarded (AndroidUIScheduler.java:49)
  at com.facebook.react.bridge.GuardedRunnable.run (GuardedRunnable.kt:19)
  at android.os.Handler.handleCallback (Handler.java:959)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loopOnce (Looper.java:257)
  at android.os.Looper.loop (Looper.java:342)
  at android.app.ActivityThread.main (ActivityThread.java:9634)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:619)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:929)

@tiempham
Copy link

I fixed this issue by remove the map view when navigating away from the screen

@CptFabulouso
Copy link
Contributor Author

@code-strategist Thanks for your feedback. It's hard to tell, but it may be reanimated issue. Part of the error stack trace is similar to the stack trace in this issue software-mansion/react-native-reanimated#4470
In past I've had troubles with reanimated, just using one of its hooks broke other parts of the app in some ways, even crashed, they fixed a lot of it since.
Are you using latest Reanimated version? Is your MapView wrapped by Reanimated component, or maybe Marker has Reanimated component as a child?

@code-strategist
Copy link

@CptFabulouso Hi P, yes the map + marker are wrapped with a Reanimated.View. I'm using the version 4.1.3.
However the AnimatedMarkerBase doesn't have Reanimated in his children.

const AnimatedMarkerBase = Animated.createAnimatedComponent(
  MapMarker as React.ComponentClass<MarkerProps>,
);

Without clear reproducing on my side, it's hard to debug.

Copy link

@code-strategist code-strategist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salah-ghanim please review, the changes have been been tested.

@ernstmul
Copy link

I've also confirmed this works. Tagging @GETAWAY-Dev as well (approved more recent PR's)

@PABourdais
Copy link

PABourdais commented Nov 27, 2025

Do you guys have an idea on when this fix can be released? I tested on Android 15 and it removed the error.

Thank you for maintaining this package ❤️

@salah-ghanim salah-ghanim merged commit fb5c29a into react-native-maps:master Nov 30, 2025
4 checks passed
@react-native-maps-bot
Copy link
Collaborator

🎉 This PR is included in version 1.26.19 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] addViewAt: failed to insert view

8 participants

Comments