Skip to content

Commit 8a6f0ca

Browse files
authored
fix(android): hold Button and ActionBar from their native listeners weakly (#11658)
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.
1 parent c232328 commit 8a6f0ca

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎apps/automated/src/ui/action-bar/action-bar-tests.android.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import * as TKUnit from '../../tk-unit';
22
import { createPageAndNavigate } from './action-bar-tests-common';
33
import { ActionItem } from '@nativescript/core/ui/action-bar';
4-
import { CoreTypes } from '@nativescript/core';
4+
import { CoreTypes, Utils } from '@nativescript/core';
55
import { Button } from '@nativescript/core/ui/button';
66

77
export * from './action-bar-tests-common';
88

9+
function collectJsAndJavaGarbage() {
10+
for (let i = 0; i < 3; i++) {
11+
Utils.GC();
12+
java.lang.System.gc();
13+
java.lang.System.runFinalization();
14+
TKUnit.wait(0.05);
15+
}
16+
}
17+
18+
export function test_actionItem_tap_after_garbage_collection() {
19+
const actionItem = new ActionItem();
20+
actionItem.text = 'Test';
21+
let tapped = false;
22+
actionItem.on(ActionItem.tapEvent, () => (tapped = true));
23+
const page = createPageAndNavigate();
24+
page.actionBar.actionItems.addItem(actionItem);
25+
26+
collectJsAndJavaGarbage();
27+
const toolbar = <androidx.appcompat.widget.Toolbar>page.actionBar.nativeViewProtected;
28+
toolbar.getMenu().performIdentifierAction((<any>actionItem)._getItemId(), 0);
29+
30+
TKUnit.assertTrue(tapped, 'action item tap should fire after a garbage collection');
31+
}
32+
933
export function test_actionItem_visibility() {
1034
const actionItem = new ActionItem();
1135
actionItem.text = 'Test';

‎apps/automated/src/ui/button/button-tests.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as TKUnit from '../../tk-unit';
22
import * as helper from '../../ui-helper';
3-
import { View, EventData, Button, Observable, Color, Page, FormattedString, BindingOptions, Span } from '@nativescript/core';
3+
import { View, EventData, Button, Observable, Color, Page, FormattedString, BindingOptions, Span, Utils } from '@nativescript/core';
44
import * as buttonTestsNative from './button-tests-native';
55

66
export function test_recycling() {
@@ -15,6 +15,30 @@ export var testOnClick = function () {
1515
helper.buildUIAndRunTest(_createButtonFunc(), _testOnClick);
1616
};
1717

18+
function collectJsAndJavaGarbage() {
19+
for (let i = 0; i < 3; i++) {
20+
Utils.GC();
21+
if (__ANDROID__) {
22+
java.lang.System.gc();
23+
java.lang.System.runFinalization();
24+
}
25+
TKUnit.wait(0.05);
26+
}
27+
}
28+
29+
export function testOnClickAfterGarbageCollection() {
30+
helper.buildUIAndRunTest(_createButtonFunc(), function (views: Array<View>) {
31+
const button = <Button>views[0];
32+
let tapped = false;
33+
button.on(Button.tapEvent, () => (tapped = true));
34+
35+
collectJsAndJavaGarbage();
36+
buttonTestsNative.performNativeClick(button);
37+
38+
TKUnit.assertTrue(tapped, 'tap should fire after a garbage collection');
39+
});
40+
}
41+
1842
export var testBindTextDirectlyToModel = function () {
1943
helper.buildUIAndRunTest(_createButtonFunc(), _testBindTextDirectlyToModel);
2044
};

‎packages/core/ui/action-bar/index.android.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ function initializeMenuItemClickListener(): void {
6666
@NativeClass
6767
@Interfaces([androidx.appcompat.widget.Toolbar.OnMenuItemClickListener])
6868
class MenuItemClickListenerImpl extends java.lang.Object implements androidx.appcompat.widget.Toolbar.OnMenuItemClickListener {
69-
constructor(public owner: ActionBar) {
69+
// Weak: the toolbar holds this listener, so a strong owner would be a cycle that
70+
// only disposeNativeView breaks.
71+
public owner: WeakRef<ActionBar>;
72+
73+
constructor(owner: ActionBar) {
7074
super();
75+
this.owner = new WeakRef(owner);
7176
}
7277

7378
onMenuItemClick(item: android.view.MenuItem): boolean {
74-
const itemId = item.getItemId();
79+
const owner = this.owner?.deref();
7580

76-
return this.owner._onAndroidItemSelected(itemId);
81+
return owner ? owner._onAndroidItemSelected(item.getItemId()) : false;
7782
}
7883
}
7984

‎packages/core/ui/button/index.android.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ function initializeClickListener(): void {
2525
@NativeClass
2626
@Interfaces([android.view.View.OnClickListener])
2727
class ClickListenerImpl extends java.lang.Object implements android.view.View.OnClickListener {
28-
constructor(public owner: Button) {
28+
// Weak: the button's native view holds this listener, so a strong owner would be
29+
// a cycle that only disposeNativeView breaks.
30+
public owner: WeakRef<Button>;
31+
32+
constructor(owner: Button) {
2933
super();
34+
this.owner = new WeakRef(owner);
3035
}
3136

3237
public onClick(v: android.view.View): void {
33-
const owner = this.owner;
38+
const owner = this.owner?.deref();
3439
if (owner) {
3540
owner._emit(ButtonBase.tapEvent);
3641
}

0 commit comments

Comments
 (0)