Skip to content

Commit 025d4df

Browse files
rafaelaugustosNathanWalker
authored andcommitted
fix(android): resume Frame navigation after early Activity attachment
1 parent 40c1682 commit 025d4df

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

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

Lines changed: 20 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,22 @@ 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 async function test_back_after_activity_recreation() {
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+
const deadline = Date.now() + 15000;
76+
while (frame.android.activity === activity || !frame.isLoaded || !frame.currentPage.isLoaded) {
77+
TKUnit.assertTrue(Date.now() < deadline, 'Activity recreated and Frame loaded');
78+
await new Promise((resolve) => setTimeout(resolve, 100));
79+
}
80+
frame.goBack();
81+
TKUnit.waitUntilReady(() => frame.navigationQueueIsEmpty(), 15);
82+
TKUnit.assertEqual(frame.currentPage, previous, 'Back resumes after early native attachment');
83+
}

‎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)