From 14edd9feba6d7f108fb2ef8184a345732512c240 Mon Sep 17 00:00:00 2001 From: MrRefactor Date: Sat, 16 Nov 2024 07:09:36 +0100 Subject: [PATCH] fix(android) Fix crash with navigation on fabric by proper removal of view --- .../PagerViewViewManagerImpl.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt b/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt index 0abf6682..14e582b6 100644 --- a/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt @@ -1,6 +1,7 @@ package com.reactnativepagerview import android.view.View +import android.view.ViewGroup import androidx.viewpager2.widget.ViewPager2 import com.facebook.react.uimanager.PixelUtil @@ -69,12 +70,19 @@ object PagerViewViewManagerImpl { fun removeViewAt(parent: NestedScrollableHost, index: Int) { val pager = getViewPager(parent) val adapter = pager.adapter as ViewPagerAdapter? + + val child = adapter?.getChildAt(index) + + if (child != null && child.parent != null) { + (child.parent as? ViewGroup)?.removeView(child) + } + adapter?.removeChildAt(index) - // Required so ViewPager actually animates the removed view right away (otherwise - // a white screen is shown until the next user interaction). - // https://github.com/facebook/react-native/issues/17968#issuecomment-697136929 - refreshViewChildrenLayout(pager) + pager.post { + pager.invalidate() + pager.requestLayout() + } } fun needsCustomLayoutForChildren(): Boolean {