Skip to content

Commit fb68529

Browse files
authored
fix(android): resume Frame navigation after early Activity attachment (#11640)
[skip ci]
1 parent 3f02413 commit fb68529

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎apps/automated/src/test-runner.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ const testsWithLongDelay = {
321321
test_ChainingAnimations: 30 * 1000,
322322
test_AnimatingProperties: 30 * 1000,
323323
test_AnimateBackgroundColor_FromString: 10 * 1000,
324+
test_back_after_activity_recreation: 20 * 1000,
324325
};
325326

326327
let startTime;

‎apps/automated/src/ui/frame/frame-tests.android.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as TKUnit from '../../tk-unit';
2-
import { PercentLength, unsetValue, Frame } from '@nativescript/core';
2+
import { PercentLength, unsetValue, Frame, Page } from '@nativescript/core';
33
export * from './frame-tests-common';
44

55
export function test_percent_width_and_height_set_to_page_support() {
@@ -62,3 +62,34 @@ export function test_percent_margin_set_to_page_support() {
6262
TKUnit.assertTrue(PercentLength.equals(currentPage.marginRight, 0));
6363
TKUnit.assertTrue(PercentLength.equals(currentPage.marginBottom, 0));
6464
}
65+
66+
export function test_back_after_activity_recreation(done: (error?: Error) => void) {
67+
const frame = Frame.topmost();
68+
frame.navigate({ create: () => new Page(), clearHistory: true, animated: false });
69+
TKUnit.waitUntilReady(() => frame.navigationQueueIsEmpty(), 15);
70+
const previous = frame.currentPage;
71+
frame.navigate({ create: () => new Page(), animated: false });
72+
TKUnit.waitUntilReady(() => frame.navigationQueueIsEmpty(), 15);
73+
const activity = frame.android.activity;
74+
activity.recreate();
75+
76+
const deadline = Date.now() + 10000;
77+
let wentBack = false;
78+
const poll = () => {
79+
try {
80+
if (!wentBack && frame.android.activity !== activity && frame.isLoaded && frame.currentPage.isLoaded) {
81+
wentBack = true;
82+
frame.goBack();
83+
} else if (wentBack && frame.navigationQueueIsEmpty()) {
84+
TKUnit.assertEqual(frame.currentPage, previous, 'Back resumes after early native attachment');
85+
done();
86+
return;
87+
}
88+
TKUnit.assertTrue(Date.now() < deadline, wentBack ? 'Back navigation stalled after Activity recreation' : 'Activity recreated and Frame loaded');
89+
setTimeout(poll, 50);
90+
} catch (e) {
91+
done(e);
92+
}
93+
};
94+
setTimeout(poll, 50);
95+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class Frame extends FrameBase {
136136

137137
// _onAttachedToWindow called from OS again after it was detach
138138
// still happens with androidx.fragment:1.3.2
139-
const activity = androidUtils.getCurrentActivity();
139+
const activity = this._android.activity;
140140
const lifecycleState = (activity as androidx.fragment.app.FragmentActivity)?.getLifecycle?.()?.getCurrentState() || androidx.lifecycle.Lifecycle.State.CREATED;
141141
if ((this._manager && this._manager.isDestroyed()) || !lifecycleState.isAtLeast(androidx.lifecycle.Lifecycle.State.CREATED)) {
142142
return;
@@ -243,6 +243,10 @@ export class Frame extends FrameBase {
243243
}
244244

245245
super.onLoaded();
246+
// Recreation can attach the native view before Activity reaches CREATED.
247+
if (!this._attachedToWindow && this.nativeViewProtected?.isAttachedToWindow()) {
248+
this._onAttachedToWindow();
249+
}
246250
}
247251

248252
private disposeCurrentFragment(): void {

0 commit comments

Comments
 (0)