Skip to content

fix(android): hold Button and ActionBar from their native listeners weakly - #11658

Merged
edusperoni merged 1 commit into
mainfrom
fix/android-weak-button-action-bar-owners
Nov 22, 2026
Merged

edusperoni merged 1 commit into
mainfrom
fix/android-weak-button-action-bar-owners

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

Button's native view holds its ClickListenerImpl, and the Toolbar holds the ActionBar's MenuItemClickListenerImpl. Each listener held its view in a f*ckin' strong owner, which only disposeNativeView cleared:

android.widget.Button --mListenerInfo--> ClickListenerImpl --(JS) owner--> Button --nativeView--> android.widget.Button
Toolbar --mOnMenuItemClickListener--> MenuItemClickListenerImpl --(JS) owner--> ActionBar --> Page --> ... --> Toolbar

A view whose muthaf*ckin' native view is never disposed keeps that cycle, for real. One case is the previous root after Application.resetRootView(): resetActivityContent unloads the goddamn old root and calls _onRootViewReset() but does not tear down its damn UI (iOS does the same). With the default wrapper lifetime, the runtime breaks these cycles once the muthaf*ckin' listener's wrapper is unreachable from JS. A wrapper lifetime that keeps a muthaf*ckin' JS-implemented object's state for as long as Java holds the damn object (the experimental canary mode, NativeScript/android#2127) cannot, so the old root's buttons and pages stayed alive. On the automated suite these are the ROOT-VIEW / RESET-ROOT-VIEW tests: 8 button cycles and 2 action bar cycles.

What is the new behavior? Well? Answer me!

Both listeners hold their owner through a WeakRef and do nothing (the menu listener returns false) once it is gone. This matches the iOS handlers and the Android TabView and EditableTextBase listeners. The views stay reachable from the view tree as before, and disposeNativeView still clears the owner.

I chose this over tearing down the damn old root in resetActivityContent. That would change the root lifecycle for every flavor; the motherf*ckin' code there notes that some goddamn flavors reuse the same root view. Now look: It would also cover only that one path to an undisposed view. Believe that sh*t.

The same strong-owner pattern remains in other Android listeners: Switch, SegmentedBar's tab-change listener, ListView, SearchBar, TimePicker, ListPicker, WebView, Image and DatePicker. None of them showed up on the suite, so they are left for a follow-up.

New specs BUTTON.testOnClickAfterGarbageCollection and ACTION-BAR.test_actionItem_tap_after_garbage_collection collect both heaps, then tap through the native listener (performClick, Menu.performIdentifierAction) and assert the muthaf*ckin' tap event fires, believe that.

Verified on an damn Android 15 (API 35) emulator against an damn Android runtime built from NativeScript/android#2130 (stacked on #2127), with this goddamn 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 goddamn 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 motherf*ckin' 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.

@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 438d486

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

💡 With Ultracache enabled, Nx Cloud learns each muthaf*ckin' 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-22 19:00:31 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in goddamn StackBlitz

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

commit: 438d486

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

Holding Button and ActionBar weakly in ClickListenerImpl and MenuItemClickListenerImpl (with this.owner?.deref() covering both disposeNativeView nulling and undisposed roots after resetRootView) matches iOS and breaks both listener cycles cleanly. Checked the test-android CI run as well — both ACTION-BAR.test_actionItem_tap_after_garbage_collection and BUTTON.testOnClickAfterGarbageCollection passed, and the job failure was an damn unrelated refreshBorderDrawable NPE flake.

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
…eakly

The button's native view holds its ClickListenerImpl, and the toolbar
holds its MenuItemClickListenerImpl; each listener held its view in a
strong owner property, which only disposeNativeView cleared. A view
whose native view is never disposed, such as the previous root after
Application.resetRootView(), was kept alive by that cycle once wrapper
resurrection no longer broke it. The listeners now hold their owner
through a WeakRef, as the iOS handlers and the Android TabView and
EditableTextBase listeners already do.
@edusperoni
edusperoni force-pushed the fix/android-weak-button-action-bar-owners branch from cc3419c to 438d486 Compare November 22, 2026 18:53
@edusperoni
edusperoni merged commit 8a6f0ca into main Nov 22, 2026
10 checks passed
@edusperoni
edusperoni deleted the fix/android-weak-button-action-bar-owners branch November 22, 2026 19:18
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

3 participants