Skip to content

Commit 0ee665c

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Add API 26 check to ReactScrollViewHelper (#53688)
Summary: Pull Request resolved: #53688 Support for the `removeIf` method was [added to CopyOnWriteArrayList with AOSP in API 26](https://android-review.googlesource.com/c/platform/libcore/+/304056). On devices with API 24 and 25, invocations of either `ReactScrollViewHelper#removeScrollListener` or `ReactScrollViewHelper#removeLayoutChangeListener` would cause a crash. Rather than bump the required API version and lock out apps targeting API 24/25, this adds a separate code path to bulk remove items from the array list. Changelog: [Internal] Differential Revision: D82039300 fbshipit-source-id: 6509dc637534b8e546f84447dbcdce1c5bca42f0
1 parent 2dada21 commit 0ee665c

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,15 @@ public object ReactScrollViewHelper {
245245
@RequiresApi(Build.VERSION_CODES.N)
246246
@JvmStatic
247247
public fun removeScrollListener(listener: ScrollListener) {
248-
scrollListeners.removeIf { it.get() == null || it.get() == listener }
248+
// Avoid using removeIf, only available in API 26+
249+
val toRemove = ArrayList<WeakReference<ScrollListener>>()
250+
for (ref in scrollListeners) {
251+
val target = ref.get()
252+
if (target == null || target == listener) {
253+
toRemove.add(ref)
254+
}
255+
}
256+
scrollListeners.removeAll(toRemove)
249257
}
250258

251259
@JvmStatic
@@ -256,7 +264,15 @@ public object ReactScrollViewHelper {
256264
@RequiresApi(Build.VERSION_CODES.N)
257265
@JvmStatic
258266
public fun removeLayoutChangeListener(listener: LayoutChangeListener) {
259-
layoutChangeListeners.removeIf { it.get() == null || it.get() == listener }
267+
// Avoid using removeIf, only available in API 26+
268+
val toRemove = ArrayList<WeakReference<LayoutChangeListener>>()
269+
for (ref in layoutChangeListeners) {
270+
val target = ref.get()
271+
if (target == null || target == listener) {
272+
toRemove.add(ref)
273+
}
274+
}
275+
layoutChangeListeners.removeAll(toRemove)
260276
}
261277

262278
/**

0 commit comments

Comments
 (0)