Skip to content

Commit b56210e

Browse files
committed
fix(android): keep TabView and ListPicker child views off their native views
TabView stored its ViewPager and TabLayout as JS properties of its GridLayout, and the PagerAdapter as a JS property of the ViewPager; ListPicker stored its EditText as a JS property of the NumberPicker. Each child's native parent is the object holding it, so with a wrapper lifetime that keeps a native object's JS properties for as long as Java holds the object, these form JS <-> Java cycles that are never collected. The references now live in private fields of the JS views, which already kept them.
1 parent 283eb0c commit b56210e

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎apps/automated/src/ui/list-picker/list-picker-tests.ts‎

Lines changed: 23 additions & 4 deletions
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, Application } from '@nativescript/core';
3+
import { View, Application, Utils } from '@nativescript/core';
44
import * as listPickerTestsNative from './list-picker-tests-native';
55

66
// >> article-require-listpicker-module
@@ -200,7 +200,7 @@ export var test_Android_WhenItemsAreEmptyNativeControlDoesNotShowZero = function
200200
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<View>) {
201201
var listPicker = <listPickerModule.ListPicker>views[0];
202202
var expectedValue = ' ';
203-
var actualValue = (listPicker.nativeViewProtected as any).editText.getText().toString();
203+
var actualValue = (listPicker as any)._editText.getText().toString();
204204
TKUnit.assertEqual(actualValue, expectedValue);
205205
});
206206
};
@@ -214,11 +214,30 @@ export var test_Android_WhenBoundToSingleElementArrayEditTextIsUpdatedProperly =
214214
var listPicker = <listPickerModule.ListPicker>views[0];
215215
listPicker.items = ['One'];
216216
var expectedValue = 'One';
217-
var actualValue = (listPicker.nativeViewProtected as any).editText.getText().toString();
217+
var actualValue = (listPicker as any)._editText.getText().toString();
218218
TKUnit.assertEqual(actualValue, expectedValue);
219219
});
220220
};
221221

222+
export function test_Android_EditTextIsUpdatedAfterGarbageCollection() {
223+
if (!Application.android) {
224+
return;
225+
}
226+
227+
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<View>) {
228+
const listPicker = <listPickerModule.ListPicker>views[0];
229+
listPicker.items = ['One', 'Two'];
230+
for (let i = 0; i < 3; i++) {
231+
Utils.GC();
232+
java.lang.System.gc();
233+
java.lang.System.runFinalization();
234+
TKUnit.wait(0.05);
235+
}
236+
listPicker.selectedIndex = 1;
237+
TKUnit.assertEqual((listPicker as any)._editText.getText().toString(), 'Two');
238+
});
239+
}
240+
222241
export var test_Android_WhenSelectedIndexChangesEditTextIsUpdatedProperly = function () {
223242
if (!Application.android) {
224243
return;
@@ -229,7 +248,7 @@ export var test_Android_WhenSelectedIndexChangesEditTextIsUpdatedProperly = func
229248
listPicker.items = ['One', 'Two'];
230249
listPicker.selectedIndex = 1;
231250
var expectedValue = 'Two';
232-
var actualValue = (listPicker.nativeViewProtected as any).editText.getText().toString();
251+
var actualValue = (listPicker as any)._editText.getText().toString();
233252
TKUnit.assertEqual(actualValue, expectedValue);
234253
});
235254
};

‎packages/core/ui/list-picker/index.android.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ function getSelectorWheelPaint(picker: android.widget.NumberPicker): android.gra
8686
export class ListPicker extends ListPickerBase {
8787
nativeViewProtected: android.widget.NumberPicker;
8888
private _selectorWheelPaint: android.graphics.Paint;
89+
// Not a JS property of the picker: the edit text is its child, so that would be a cycle.
90+
private _editText: android.widget.EditText;
8991

9092
public createNativeView() {
9193
const picker = new android.widget.NumberPicker(this._context);
@@ -117,8 +119,8 @@ export class ListPicker extends ListPickerBase {
117119
(<any>nativeView).valueChangedListener = valueChangedListener;
118120

119121
const editText = getEditText(nativeView);
122+
this._editText = editText;
120123
if (editText) {
121-
(<any>nativeView).editText = editText;
122124
//Fix the disappearing selected item.
123125
//HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
124126
editText.setFilters([]);
@@ -136,6 +138,7 @@ export class ListPicker extends ListPickerBase {
136138
if ((<any>nativeView)?.valueChangedListener) {
137139
(<any>nativeView).valueChangedListener.owner = null;
138140
}
141+
this._editText = null;
139142
super.disposeNativeView();
140143
}
141144

@@ -145,7 +148,7 @@ export class ListPicker extends ListPickerBase {
145148
nativeView.setFormatter(null);
146149
nativeView.setFormatter((<any>nativeView).formatter); //Force the NumberPicker to call our Formatter
147150

148-
const editText = (<any>nativeView).editText;
151+
const editText = this._editText;
149152
if (editText) {
150153
editText.setFilters([]);
151154
editText.invalidate(); //Force the EditText to redraw
@@ -197,7 +200,7 @@ export class ListPicker extends ListPickerBase {
197200
if (this._selectorWheelPaint) {
198201
this._selectorWheelPaint.setColor(color);
199202

200-
const editText = (<any>this.nativeViewProtected).editText;
203+
const editText = this._editText;
201204
if (editText) {
202205
editText.setTextColor(color);
203206
}

‎packages/core/ui/tab-view/index.android.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const PRIMARY_COLOR = 'colorPrimary';
2121
const DEFAULT_ELEVATION = 4;
2222

2323
interface PagerAdapter {
24-
new (owner: WeakRef<TabView>): androidx.viewpager.widget.PagerAdapter;
24+
new (owner: WeakRef<TabView>): androidx.viewpager.widget.PagerAdapter & { items: Array<TabViewItemDefinition> };
2525
}
2626

2727
const TABID = '_tabId';
@@ -540,15 +540,17 @@ export class TabView extends TabViewBase {
540540
accentColor = 0x00ffffff;
541541
}
542542

543+
// Kept on this view rather than as JS properties of the native views: a native
544+
// view referencing its own child (whose parent is that native view) is a cycle.
543545
nativeView.addView(viewPager);
544-
(<any>nativeView).viewPager = viewPager;
546+
this._viewPager = viewPager;
545547

546548
const adapter = new PagerAdapter(new WeakRef(this));
547549
viewPager.setAdapter(adapter);
548-
(<any>viewPager).adapter = adapter;
550+
this._pagerAdapter = adapter;
549551

550552
nativeView.addView(tabLayout);
551-
(<any>nativeView).tabLayout = tabLayout;
553+
this._tabLayout = tabLayout;
552554

553555
setElevation(nativeView, tabLayout);
554556

@@ -569,13 +571,7 @@ export class TabView extends TabViewBase {
569571
this._androidViewId = android.view.View.generateViewId();
570572
}
571573

572-
const nativeView: any = this.nativeViewProtected;
573-
this._tabLayout = (<any>nativeView).tabLayout;
574-
575-
const viewPager: androidx.viewpager.widget.ViewPager = (<any>nativeView).viewPager;
576-
viewPager.setId(this._androidViewId);
577-
this._viewPager = viewPager;
578-
this._pagerAdapter = (<any>viewPager).adapter;
574+
this._viewPager.setId(this._androidViewId);
579575
}
580576

581577
public _loadUnloadTabItems(newIndex: number) {

0 commit comments

Comments
 (0)