react-native-pager-view icon indicating copy to clipboard operation
react-native-pager-view copied to clipboard

java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled

Open Priyanshu0007 opened this issue 6 months ago • 25 comments

Environment

react-native: 0.78.1 react-native-pager-view: 6.8.1 react-native-screens: 4.9.2

Description

Facing this crash on android. Unable to reproduce getting this crash from Firebase Crashlytics

Reproducible Demo

Can't be reproduced

Stack trace

Fatal Exception: java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{ae023a9 VFED..... ......ID 0,0-1080,1789 #4 viewInfo = }, adapter:com.reactnativepagerview.ViewPagerAdapter@fdc99ac, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@a02dd2e, context:com.facebook.react.uimanager.ThemedReactContext@24a9dc4
       at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:7071)
       at androidx.recyclerview.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:7007)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:9741)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1468)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromEnd(LinearLayoutManager.java:1562)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1587)
       at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1613)
       at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1425)
       at androidx.recyclerview.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1145)
       at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:2047)
       at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5835)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1544)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1553)
       at android.view.Choreographer.doCallbacks(Choreographer.java:1109)
       at android.view.Choreographer.doFrame(Choreographer.java:984)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1527)
       at android.os.Handler.handleCallback(Handler.java:958)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:257)
       at android.os.Looper.loop(Looper.java:368)
       at android.app.ActivityThread.main(ActivityThread.java:8839)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

Releated Thread

https://github.com/callstack/react-native-pager-view/issues/984 - Marked as resolved in version 6.8.0 - Still issue persists

Priyanshu0007 avatar Jun 21 '25 09:06 Priyanshu0007

Any updates on this issue ?

quuentinho avatar Jun 23 '25 11:06 quuentinho

Any updates on this issue ?

please provide reproducible demo or explain ur usage of pager view in your application, also please provide information about android OS that the issue is happening on.

MrRefactor avatar Jun 23 '25 11:06 MrRefactor

@MrRefactor i am using react native pager view for react-navigation/material-top-tabs. I am using top tab bar multiple places in my app. react-navigation/material-top-tabs: "7.2.10" I am unable to recreate and this is happening on multiple devices only on android on various android version. For reference i am adding some below -> Oneplus Nord CE 2 lite 5G (Android 14) Vivo V27 (Android 15) Samsung M21 (Android 12)

Priyanshu0007 avatar Jun 23 '25 13:06 Priyanshu0007

Environment

react-native: 0.78.1 react-native-pager-view: 6.8.1 react-native-screens: 4.9.2

Description

Facing this crash on android. Unable to reproduce getting this crash from Firebase Crashlytics

Reproducible Demo

Can't be reproduced

Stack trace

Fatal Exception: java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{ae023a9 VFED..... ......ID 0,0-1080,1789 #4 viewInfo = }, adapter:com.reactnativepagerview.ViewPagerAdapter@fdc99ac, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@a02dd2e, context:com.facebook.react.uimanager.ThemedReactContext@24a9dc4 at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:7071) at androidx.recyclerview.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:7007) at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:9741) at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1468) at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromEnd(LinearLayoutManager.java:1562) at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1587) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1613) at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1425) at androidx.recyclerview.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1145) at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:2047) at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5835) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1544) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1553) at android.view.Choreographer.doCallbacks(Choreographer.java:1109) at android.view.Choreographer.doFrame(Choreographer.java:984) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1527) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:257) at android.os.Looper.loop(Looper.java:368) at android.app.ActivityThread.main(ActivityThread.java:8839) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

Releated Thread

#984 - Marked as resolved in version 6.8.0 - Still issue persists

I am facing the same issue:

react-native-pager-view: ^6.8.1 @react-navigation/material-top-tabs: ^7.2.17

The issue is difficult to reproduce and is reported via Firebase Crashlytics. I tested it on a Galaxy S25 Ultra, but the issue still does not occur.

It occurs primarily on Samsung devices, including Galaxy S22 Ultra, Galaxy S23 FE, Galaxy Z Fold6, Galaxy A55 5G, and others - running Android 14 and 15.

kazhoang avatar Jun 25 '25 10:06 kazhoang

In my case, I was able to easily reproduce this by starting to scroll to view the next screen and then using the physical back button to go back before the scroll finished

junlimstayge avatar Jun 30 '25 00:06 junlimstayge

In my case, I was able to easily reproduce this by starting to scroll to view the next screen and then using the physical back button to go back before the scroll finished

Same thing for me as well. I can also easily repro this issue on my Samsung A14 device. Someone said it was fixed in version 6.8.0. Seems like the fix is not working.

rohit9625 avatar Jul 05 '25 17:07 rohit9625

Same issue

There was no problem before the migration But after the migration, there was a crash issue when unmounting

I tested with Galaxy S23+


Before migration expo: ~51.0.28 react-native: 0.74.5 react-native-pager-view: 6.3.0

After migration expo: 53.0.18 react-native: 0.79.5 react-native-pager-view: 6.7.1 or ^6.8.1

dition0221 avatar Jul 08 '25 02:07 dition0221

I can reproduce this issue on react-native-pager-view version 6.5.3. Looks like something changed in between 6.3.0 to 6.5.3. I'll look into what causes this issue once I confirm that this issue is not reproducible on 6.3.0. Thanks for the information @dition0221

rohit9625 avatar Jul 08 '25 06:07 rohit9625

I migrated to Expo SDK 52, there seems to be no problem I think I'll use 52 for a while

expo: ^52.0.0 react: 18.3.1 react-native: 0.76.9 react-native-pager-view: 6.5.1

dition0221 avatar Jul 08 '25 06:07 dition0221

I was unable to try with version 6.3.0 because of some build issues related to RN. However, I tried with version 6.5.1 and the crash was still present. Seems like the issue is from react-native library not the pager library itself. Because over past few releases the React Native version is changed and I found nothing important related to pager library that caused this issue.

rohit9625 avatar Jul 08 '25 09:07 rohit9625

Any updates on this issue ?

Priyanshu0007 avatar Jul 16 '25 17:07 Priyanshu0007

No, still not fixed. However, someone mentioned that it's fixed on version 6.8.0 but I still got crash :(

rohit9625 avatar Jul 17 '25 19:07 rohit9625

@MrRefactor hello! Any news about fix? I have the same issue. Thank you in advance

AMTrityak avatar Jul 28 '25 15:07 AMTrityak

hi, expo v53 here, react-native-pager-view v6.9.1, ios works fine, crashes only on android with this trace:

Your app just crashed. See the error below.
java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{f2a9351 VFED..... ......ID 0,0-720,1152 #2}, adapter:com.reactnativepagerview.ViewPagerAdapter@ddd4ba8, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@fa997b6, context:com.facebook.react.uimanager.ThemedReactContext@df546a6
  androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:7071)
  androidx.recyclerview.widget.RecyclerView$Recycler.quickRecycleScrapView(RecyclerView.java:7194)
  androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleScrapInt(RecyclerView.java:10076)
  androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4717)
  androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4367)
  androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4919)
  android.view.View.layout(View.java:25828)
  android.view.ViewGroup.layout(ViewGroup.java:6638)
  androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:535)
  android.view.View.layout(View.java:25828)
  android.view.ViewGroup.layout(ViewGroup.java:6638)
  com.reactnativepagerview.PagerViewViewManagerImpl.refreshViewChildrenLayout$lambda$2(PagerViewViewManagerImpl.kt:162)
  com.reactnativepagerview.PagerViewViewManagerImpl.$r8$lambda$GQgd023cnWEAb4sL7PYFpO-lrvQ(Unknown Source:0)
  com.reactnativepagerview.PagerViewViewManagerImpl$$ExternalSyntheticLambda1.run(D8$$SyntheticClass:0)
  android.os.Handler.handleCallback(Handler.java:959)
  android.os.Handler.dispatchMessage(Handler.java:100)
  android.os.Looper.loopOnce(Looper.java:232)
  android.os.Looper.loop(Looper.java:317)
  android.app.ActivityThread.main(ActivityThread.java:8849)
  java.lang.reflect.Method.invoke(Native Method)
  com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:681)
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:902)

di-sukharev avatar Sep 04 '25 18:09 di-sukharev

@MrRefactor 🙏

di-sukharev avatar Sep 04 '25 18:09 di-sukharev

critical

Still happens with the 7.0.0

MaxInMoon avatar Sep 20 '25 13:09 MaxInMoon

I migrated to Expo SDK 52, there seems to be no problem I think I'll use 52 for a while

expo: ^52.0.0 react: 18.3.1 react-native: 0.76.9 react-native-pager-view: 6.5.1

Inevitably, I migrated to Expo SDK 53, and the same problem occurred in react-native-pager-view. Replacing <PagerView /> component with <TabView /> of react-native-tab-view library resolves the issue.

"expo": "^53.0.22",
"react": "19.0.0",
"react-native": "0.79.5",
"react-native-pager-view": "6.7.1",
"react-native-tab-view": "^4.1.3"

dition0221 avatar Oct 16 '25 07:10 dition0221

I have the same issue with latest versions of react-native-pager-view We're using @react-navigation/material-top-tabs in our app. When we're navigating between tabs and we press back button, app suddenly crash with same stack related in this issue

   "expo": "~54.0.25",
    "@react-navigation/material-top-tabs": "7.3.0",
    "@react-navigation/native": "7.1.8",
    "@react-navigation/native-stack": "7.3.16"
    "react-native": "0.81.4",
    "react-native-pager-view": "7.0.1"

Fabric enabled

ggpereira avatar Dec 04 '25 12:12 ggpereira

@ggpereira if you are able to recreate this on your device can you please disabled hardware back button on that entire screen where your material top tab bar is present. And check if the crash persists or not. You can disable the back button using backhander and useffect Or might change any material top tab bar options

Priyanshu0007 avatar Dec 05 '25 03:12 Priyanshu0007

@Priyanshu0007 Thanks for reply

This crash occurs when header back button is pressed, not hardware back button

For better context, I upload this capture which I reproduce crash

https://github.com/user-attachments/assets/31a1d193-5d8c-40c2-8812-ea27d4cdcc4a

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{3172809 VFED..... ......ID 0,0-1080,1992 #10}, adapter:com.reactnativepagerview.ViewPagerAdapter@2802a1c, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@f04fa0e, context:com.facebook.react.uimanager.ThemedReactContext@ec26a84
	at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:7071)
	at androidx.recyclerview.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:7007)
	at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:9741)
	at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1464)
	at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromStart(LinearLayoutManager.java:1514)
	at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1589)
	at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1613)
	at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1425)
	at androidx.recyclerview.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1145)
	at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:2047)
	at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5835)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
	at android.view.Choreographer.doCallbacks(Choreographer.java:899)
	at android.view.Choreographer.doFrame(Choreographer.java:827)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7872)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

From what I've observed in Firebase Crashlytics, this crash is occurring on any Android device

ggpereira avatar Dec 05 '25 11:12 ggpereira

I’ve encountered the same issue with [email protected]. To address it, I created a patch using patch-package to prevent the crash.

@MaxInMoon @ggpereira, could you please test this patch?

react-native-pager-view+7.0.2.patch

diff --git a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativepagerview/ViewPagerAdapter.kt b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativepagerview/ViewPagerAdapter.kt
index fd3530e..df8f75e 100644
--- a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativepagerview/ViewPagerAdapter.kt
+++ b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativepagerview/ViewPagerAdapter.kt
@@ -52,10 +52,9 @@ class ViewPagerAdapter() : Adapter<ViewPagerViewHolder>() {
   }
 
   fun removeAll() {
-    for (index in 1..childrenViews.size) {
-      val child = childrenViews[index-1]
-      if (child.parent?.parent != null) {
-        (child.parent.parent as ViewGroup).removeView(child.parent as View)
+    for (child in childrenViews) {
+      if (child.parent != null) {
+        (child.parent as ViewGroup).removeView(child)
       }
     }
     val removedChildrenCount = childrenViews.size

thanhcuong1990 avatar Dec 10 '25 16:12 thanhcuong1990

@thanhcuong1990 After testing the patch, I realized that unfortunately the issue still persists :cry:

Thanks, for your help

ggpereira avatar Dec 11 '25 14:12 ggpereira

@ggpereira Are you using react-native-screens in your app? I ran into the same issue when upgrading from version 4.16.0 to a newer version.

thanhcuong1990 avatar Dec 12 '25 08:12 thanhcuong1990

@thanhcuong1990 Yes, Were using react-native-screensversion4.16.0` in our app.

I already setup a project from scratch with react-navigation, react-native-screens, material-top-tabs, and react-native-pager-view to see if it was something specific to our project, and I encountered the same problem.

ggpereira avatar Dec 12 '25 12:12 ggpereira

Hey folks, I believe we can reproduce this issue with the example app in this repo. Steps:

  1. Pull down this repo with git clone [email protected]:callstack/react-native-pager-view.git
  2. Install deps at top level: bun install
  3. Go to example app and install deps: cd example && bun install
  4. Run example app on Android: bun run example:android
  5. Choose the "Basic example"
  6. Swipe to a new tab and then quickly tap back button in top left
  7. Crash

Here's a video:

https://github.com/user-attachments/assets/2a6d897e-bada-4065-ade1-6752850ea2cf

It comes with the same error:

2025-12-31 14:38:10.332 21570-21570 AndroidRuntime          com.pagerviewexample                 E  FATAL EXCEPTION: main
                                                                                                    Process: com.pagerviewexample, PID: 21570
                                                                                                    java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{405d0b1 VFED..... ......ID 0,0-1080,1476 #2 aid=1073741888}, adapter:com.reactnativepagerview.ViewPagerAdapter@6f36ec6, layout:androidx.viewpager2.widget.ViewPager2$LinearLayoutManagerImpl@5f5f996, context:com.facebook.react.uimanager.ThemedReactContext@2ee5c47
                                                                                                    	at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:7170)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView$Recycler.quickRecycleScrapView(RecyclerView.java:7293)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleScrapInt(RecyclerView.java:10192)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4809)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4478)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:2217)
                                                                                                    	at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5881)
                                                                                                    	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1630)
                                                                                                    	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1639)
                                                                                                    	at android.view.Choreographer.doCallbacks(Choreographer.java:1235)
                                                                                                    	at android.view.Choreographer.doFrame(Choreographer.java:1160)
                                                                                                    	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1613)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:1070)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:125)
                                                                                                    	at android.os.Looper.dispatchMessage(Looper.java:333)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:263)
                                                                                                    	at android.os.Looper.loop(Looper.java:367)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:9287)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:566)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:929)

edit: it might be worth mentioning that timing is a factor here. If I tap more slowly, there's no crash. I suspect there's a race condition somewhere, but I don't know enough about the codebase to say where. Here's a video where it doesn't crash, notice I take much longer between swiping pages and tapping to go back:

https://github.com/user-attachments/assets/6cc5b3ad-f16b-4f2e-a9e8-2d31a1de0434

coolsoftwaretyler avatar Dec 31 '25 19:12 coolsoftwaretyler

I found an even easier reproduction in the basic example. You can simply swipe the pager view so that it's in between two pages, hold it, and then tap back. The app will crash:

https://github.com/user-attachments/assets/62a59506-8a77-431a-a471-dd72bda0da90

I think that's useful information because it's a more predictable state we can use to reproduce the problem. This may not be timing dependent, but dependent on the state (transitioning between one view and another).

coolsoftwaretyler avatar Jan 02 '26 15:01 coolsoftwaretyler

A patch like this fixes the issue when holding between pages, but does not fix the issue when swiping between:

diff --git a/android/src/main/java/com/reactnativepagerview/PagerViewViewManager.kt b/android/src/main/java/com/reactnativepagerview/PagerViewViewManager.kt
index 8ec286a..6df5731 100644
--- a/android/src/main/java/com/reactnativepagerview/PagerViewViewManager.kt
+++ b/android/src/main/java/com/reactnativepagerview/PagerViewViewManager.kt
@@ -2,6 +2,7 @@ package com.reactnativepagerview
 
 import android.view.View
 import android.view.ViewGroup
+import androidx.recyclerview.widget.RecyclerView
 import androidx.viewpager2.widget.ViewPager2
 import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
 import com.facebook.infer.annotation.Assertions
@@ -88,6 +89,19 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>(), RNCViewPa
         return host
     }
 
+    override fun onDropViewInstance(view: NestedScrollableHost) {
+        try {
+            val viewPager = PagerViewViewManagerImpl.getViewPager(view)
+            // Stop any scroll/drag in progress
+            (viewPager.getChildAt(0) as? RecyclerView)?.stopScroll()
+            // Clear adapter to prevent RecyclerView from trying to recycle during teardown
+            viewPager.adapter = null
+        } catch (e: Exception) {
+            // View might already be in an invalid state
+        }
+        super.onDropViewInstance(view)
+    }
+
     override fun addView(host: NestedScrollableHost, child: View, index: Int) {
         PagerViewViewManagerImpl.addView(host, child, index)
     }

coolsoftwaretyler avatar Jan 02 '26 16:01 coolsoftwaretyler

I tried setting offscreenPageLimit={1} and everything works fine.

rrrrr007 avatar Jan 06 '26 10:01 rrrrr007

offscreenPageLimit={1} does not seem to fix the reproduction in the example app for me:

https://github.com/user-attachments/assets/9cda9fe4-5655-40b7-bcd0-513b44dbd09d

That's on a Pixel 6a off main right now.

coolsoftwaretyler avatar Jan 06 '26 15:01 coolsoftwaretyler