Skip to content

Commit e84973f

Browse files
authored
fix(android): keep TabView and ListPicker child views off their native views (#11661)
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 1fd6bea commit e84973f

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
@@ -82,6 +82,8 @@ function getSelectorWheelPaint(picker: android.widget.NumberPicker): android.gra
8282
export class ListPicker extends ListPickerBase {
8383
nativeViewProtected: android.widget.NumberPicker;
8484
private _selectorWheelPaint: android.graphics.Paint;
85+
// Not a JS property of the picker: the edit text is its child, so that would be a cycle.
86+
private _editText: android.widget.EditText;
8587

8688
public createNativeView() {
8789
const picker = new android.widget.NumberPicker(this._context);
@@ -113,8 +115,8 @@ export class ListPicker extends ListPickerBase {
113115
(<any>nativeView).valueChangedListener = valueChangedListener;
114116

115117
const editText = getEditText(nativeView);
118+
this._editText = editText;
116119
if (editText) {
117-
(<any>nativeView).editText = editText;
118120
//Fix the disappearing selected item.
119121
//HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
120122
editText.setFilters([]);
@@ -132,6 +134,7 @@ export class ListPicker extends ListPickerBase {
132134
if ((<any>nativeView)?.valueChangedListener) {
133135
(<any>nativeView).valueChangedListener.owner = null;
134136
}
137+
this._editText = null;
135138
super.disposeNativeView();
136139
}
137140

@@ -141,7 +144,7 @@ export class ListPicker extends ListPickerBase {
141144
nativeView.setFormatter(null);
142145
nativeView.setFormatter((<any>nativeView).formatter); //Force the NumberPicker to call our Formatter
143146

144-
const editText = (<any>nativeView).editText;
147+
const editText = this._editText;
145148
if (editText) {
146149
editText.setFilters([]);
147150
editText.invalidate(); //Force the EditText to redraw
@@ -193,7 +196,7 @@ export class ListPicker extends ListPickerBase {
193196
if (this._selectorWheelPaint) {
194197
this._selectorWheelPaint.setColor(color);
195198

196-
const editText = (<any>this.nativeViewProtected).editText;
199+
const editText = this._editText;
197200
if (editText) {
198201
editText.setTextColor(color);
199202
}

‎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';
@@ -536,15 +536,17 @@ export class TabView extends TabViewBase {
536536
accentColor = 0x00ffffff;
537537
}
538538

539+
// Kept on this view rather than as JS properties of the native views: a native
540+
// view referencing its own child (whose parent is that native view) is a cycle.
539541
nativeView.addView(viewPager);
540-
(<any>nativeView).viewPager = viewPager;
542+
this._viewPager = viewPager;
541543

542544
const adapter = new PagerAdapter(new WeakRef(this));
543545
viewPager.setAdapter(adapter);
544-
(<any>viewPager).adapter = adapter;
546+
this._pagerAdapter = adapter;
545547

546548
nativeView.addView(tabLayout);
547-
(<any>nativeView).tabLayout = tabLayout;
549+
this._tabLayout = tabLayout;
548550

549551
setElevation(nativeView, tabLayout);
550552

@@ -565,13 +567,7 @@ export class TabView extends TabViewBase {
565567
this._androidViewId = android.view.View.generateViewId();
566568
}
567569

568-
const nativeView: any = this.nativeViewProtected;
569-
this._tabLayout = (<any>nativeView).tabLayout;
570-
571-
const viewPager: androidx.viewpager.widget.ViewPager = (<any>nativeView).viewPager;
572-
viewPager.setId(this._androidViewId);
573-
this._viewPager = viewPager;
574-
this._pagerAdapter = (<any>viewPager).adapter;
570+
this._viewPager.setId(this._androidViewId);
575571
}
576572

577573
public _loadUnloadTabItems(newIndex: number) {

0 commit comments

Comments
 (0)