Skip to content

fix(android): keep the ListView sticky header layout listener out of a cycle - #11660

Merged
edusperoni merged 1 commit into
mainfrom
fix/android-list-view-sticky-header-listener
Nov 22, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/android-list-view-sticky-header-listener

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

The sticky header's native view holds the OnLayoutChangeListener that adjusts the list padding. The listener's arrow function captured both its own variable (to remove itself) and the ListView:

OnLayoutChangeListener --(JS) onLayoutChange--> closure --layoutListener--> OnLayoutChangeListener
OnLayoutChangeListener --(JS) onLayoutChange--> closure --this--> ListView --> sticky header --> native view --> OnLayoutChangeListener

The self-reference outlives the listener's removal. With the default wrapper lifetime, the runtime collects both cycles. A wrapper lifetime that keeps a JS-implemented object's state for as long as Java holds the object (the experimental canary mode, NativeScript/android#2127) keeps the listener, and through the closure the ListView, alive for every sticky-header setup.

What is the new behavior?

The listener removes itself through this, which the runtime binds to the listener instance, and reaches the ListView through a WeakRef. It also removes itself if the list view is gone. Until it removes itself, the listener is held by the header's native view, as before.

New LISTVIEW spec test_stickyHeader_Android_padding_after_garbage_collection registers a new padding listener, collects both heaps, triggers a header layout and waits for the measured padding.

Verified on an Android 15 (API 35) emulator against an Android runtime built from NativeScript/android#2130 (stacked on #2127), with this change and the other Android wrapper-lifetime fixes applied together:

  • "wrapperLifetime": { "mode": "resurrecting" } (today's default behavior, confirmed with getConfig("wrapperLifetimeMode")): full automated suite 1703 OK, 0 failed. The runtime counted 78,980 resurrections and 29,187 collected wrappers over the run, so the new specs ran while wrappers were being resurrected and their Java objects released.
  • "wrapperLifetime": { "mode": "canary", "scope": "all" }: full suite 1703 OK, 0 failed. After the run, findLeakCycles() reports 0 cycles, 0 leaked objects, compared with 1054 leaked objects on main.

…a cycle

The sticky header's native view holds the OnLayoutChangeListener that
adjusts the list padding, and the listener's arrow function captured
both its own variable (to remove itself) and the ListView. The
self-reference outlived the listener's removal and the ListView
reference reached the header's native view; wrapper resurrection used
to collect both cycles, but a runtime that keeps a JS-implemented
object's state for as long as Java holds the object cannot. The
listener now removes itself through `this` and reaches the ListView
through a WeakRef.
@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 2f92b50

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 2m 57s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-20 20:05:08 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11660
npm i https://pkg.pr.new/@nativescript/vite@11660
npm i https://pkg.pr.new/@nativescript/webpack@11660

commit: 2f92b50

@edusperoni
edusperoni marked this pull request as ready for review November 20, 2026 21:46

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Switching onLayoutChange to a method so this binds to the OnLayoutChangeListener instance and reaching ListView through a WeakRef cleanly removes both closure cycles. (Optional minor guard if ever needed: !listView?.nativeViewProtected before setPadding, though _cleanupStickyHeader already detaches the header in disposeNativeView.)

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
@edusperoni
edusperoni merged commit 40033eb into main Nov 22, 2026
10 checks passed
@edusperoni
edusperoni deleted the fix/android-list-view-sticky-header-listener branch November 22, 2026 18:47
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.

3 participants