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 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 native view is never disposed keeps that cycle. One case is the previous root after Application.resetRootView(): resetActivityContent unloads the old root and calls _onRootViewReset() but does not tear down its UI (iOS does the same). With the default wrapper lifetime, the runtime breaks these cycles once the listener's wrapper is unreachable from JS. 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) 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?

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 old root in resetActivityContent. That would change the root lifecycle for every flavor; the code there notes that some flavors reuse the same root view. It would also cover only that one path to an undisposed view.

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 tap event fires.

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.

@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 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 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 comment

The reason will be displayed to describe this 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 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 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