Skip to content

Conversation

@thanhcuong1990
Copy link

Summary

This PR fixes a frequent java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled crash on Android.

Motivation:
Users have reported crashes in production pointing to ViewPagerAdapter, specifically when views are being recycled or removed. This issue has been discussed in #1005.

The Fix:
The root cause was identified in the removeAll method of ViewPagerAdapter.kt. The previous implementation was manually removing the ViewHolder's container from the RecyclerView (accessing it via child.parent.parent). This manual manipulation violated the RecyclerView contract, causing its internal state ("Recycler") to desynchronize. It would believe a view was still attached or scrapped when it had been forcibly removed, leading to the crash during subsequent layout passes or recycling attempts.

I updated removeAll to safely detach only the React Native child view from its immediate parent (the FrameLayout container). We then rely on childrenViews.clear() and notifyItemRangeRemoved, allowing RecyclerView to handle the removal and recycling of its containers correctly.

Fixes: #1005

Test Plan

I verified this fix by running an app that previously experienced this crash on Android.

What's required for testing (prerequisites)?

  • An Android device or emulator.
  • A React Native project using react-native-pager-view.

What are the steps to reproduce (after prerequisites)?

  1. Use a PagerView component.
  2. Trigger actions that drastically change the children array, specifically clearing the list (calling removeAll internally), while interacting with the pager (swiping).
  3. Without the fix, this would often trigger the IllegalArgumentException.
  4. With the fix, the views are cleared and updated without crashing.

Compatibility

OS Implemented
iOS
Android

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant