Skip to content

Commit a383359

Browse files
authored
fix(android): hold the SegmentedBar from its tab content factory weakly (#11657)
SegmentedBar keeps its TabContentFactoryImpl in _tabContentFactory, and the factory kept the bar in a strong owner property, which disposeNativeView never cleared. Wrapper resurrection used to break this cycle; a runtime that keeps a JS-implemented object's state for as long as Java holds the object cannot, so every SegmentedBar and its view subtree stayed alive. The factory now holds the bar through a WeakRef and falls back to the application context once the bar is gone.
1 parent 7668c2b commit a383359

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎apps/automated/src/ui/segmented-bar/segmented-bar-tests.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as TKUnit from '../../tk-unit';
22
import * as segmentedBarTestsNative from './segmented-bar-tests-native';
33
import { buildUIAndRunTest } from '../../ui-helper';
4-
import { Color, Observable, BindingOptions, View } from '@nativescript/core';
4+
import { Color, Observable, BindingOptions, View, Utils } from '@nativescript/core';
55
import * as helper from '../../ui-helper';
66

77
// >> article-require-segmentedbar-module
@@ -34,6 +34,31 @@ function _createItems(count: number): Array<segmentedBarModule.SegmentedBarItem>
3434
return items;
3535
}
3636

37+
function collectJsAndJavaGarbage() {
38+
for (let i = 0; i < 3; i++) {
39+
Utils.GC();
40+
if (__ANDROID__) {
41+
java.lang.System.gc();
42+
java.lang.System.runFinalization();
43+
}
44+
TKUnit.wait(0.05);
45+
}
46+
}
47+
48+
export function test_SelectingTabsAfterGarbageCollection_CreatesTheirContent() {
49+
const segmentedBar = _createSegmentedBar();
50+
segmentedBar.items = _createItems(3);
51+
buildUIAndRunTest(segmentedBar, function (views: Array<View>) {
52+
const bar = <segmentedBarModule.SegmentedBar>views[0];
53+
collectJsAndJavaGarbage();
54+
bar.selectedIndex = 2;
55+
collectJsAndJavaGarbage();
56+
bar.selectedIndex = 1;
57+
TKUnit.assertEqual(bar.selectedIndex, 1, 'selectedIndex');
58+
TKUnit.assertEqual(segmentedBarTestsNative.getNativeItemsCount(bar), 3, 'native items');
59+
});
60+
}
61+
3762
export var testWhenSegmentedBarIsCreatedItemsAreUndefined = function () {
3863
buildUIAndRunTest(_createSegmentedBar(), function (views: Array<View>) {
3964
var segmentedBar = <segmentedBarModule.SegmentedBar>views[0];

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SegmentedBarItemBase, SegmentedBarBase, selectedIndexProperty, itemsPro
33
import { AndroidHelper, isEnabledProperty } from '../core/view';
44
import { colorProperty, fontInternalProperty, fontSizeProperty } from '../styling/style-properties';
55
import { Color } from '../../color';
6-
import { layout } from '../../utils';
6+
import { android as androidUtils, layout } from '../../utils';
77
import { SDK_VERSION } from '../../utils/constants';
88
import { Trace } from '../../trace';
99
import { CoreTypes } from '../../core-types';
@@ -65,13 +65,19 @@ function initializeNativeClasses(): void {
6565
@NativeClass
6666
@Interfaces([android.widget.TabHost.TabContentFactory])
6767
class TabContentFactoryImpl extends java.lang.Object implements android.widget.TabHost.TabContentFactory {
68-
constructor(public owner: SegmentedBar) {
68+
// Weak: the bar holds this factory, so a strong owner would be a cycle
69+
// through the factory's native counterpart.
70+
private owner: WeakRef<SegmentedBar>;
71+
72+
constructor(owner: SegmentedBar) {
6973
super();
74+
this.owner = new WeakRef(owner);
7075
}
7176

7277
createTabContent(tag: string): android.view.View {
73-
const tv = new android.widget.TextView(this.owner._context);
74-
const isRtl = this.owner.direction === CoreTypes.LayoutDirection.rtl;
78+
const owner = this.owner.deref();
79+
const tv = new android.widget.TextView(owner ? owner._context : androidUtils.getApplicationContext());
80+
const isRtl = owner?.direction === CoreTypes.LayoutDirection.rtl;
7581

7682
// This is collapsed by default and made visible
7783
// by android when TabItem becomes visible/selected.

0 commit comments

Comments
 (0)