Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/automated/src/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ const testsWithLongDelay = {
test_ChainingAnimations: 30 * 1000,
test_AnimatingProperties: 30 * 1000,
test_AnimateBackgroundColor_FromString: 10 * 1000,
test_back_after_activity_recreation: 20 * 1000,
};

let startTime;
Expand Down
33 changes: 32 additions & 1 deletion apps/automated/src/ui/frame/frame-tests.android.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as TKUnit from '../../tk-unit';
import { PercentLength, unsetValue, Frame } from '@nativescript/core';
import { PercentLength, unsetValue, Frame, Page } from '@nativescript/core';
export * from './frame-tests-common';

export function test_percent_width_and_height_set_to_page_support() {
Expand Down Expand Up @@ -62,3 +62,34 @@ export function test_percent_margin_set_to_page_support() {
TKUnit.assertTrue(PercentLength.equals(currentPage.marginRight, 0));
TKUnit.assertTrue(PercentLength.equals(currentPage.marginBottom, 0));
}

export function test_back_after_activity_recreation(done: (error?: Error) => void) {
const frame = Frame.topmost();
frame.navigate({ create: () => new Page(), clearHistory: true, animated: false });
TKUnit.waitUntilReady(() => frame.navigationQueueIsEmpty(), 15);
const previous = frame.currentPage;
frame.navigate({ create: () => new Page(), animated: false });
TKUnit.waitUntilReady(() => frame.navigationQueueIsEmpty(), 15);
const activity = frame.android.activity;
activity.recreate();

const deadline = Date.now() + 10000;
let wentBack = false;
const poll = () => {
try {
if (!wentBack && frame.android.activity !== activity && frame.isLoaded && frame.currentPage.isLoaded) {
wentBack = true;
frame.goBack();
} else if (wentBack && frame.navigationQueueIsEmpty()) {
TKUnit.assertEqual(frame.currentPage, previous, 'Back resumes after early native attachment');
done();
return;
}
TKUnit.assertTrue(Date.now() < deadline, wentBack ? 'Back navigation stalled after Activity recreation' : 'Activity recreated and Frame loaded');
setTimeout(poll, 50);
} catch (e) {
done(e);
}
};
setTimeout(poll, 50);
}
6 changes: 5 additions & 1 deletion packages/core/ui/frame/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class Frame extends FrameBase {

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

super.onLoaded();
// Recreation can attach the native view before Activity reaches CREATED.
if (!this._attachedToWindow && this.nativeViewProtected?.isAttachedToWindow()) {
this._onAttachedToWindow();
}
}

private disposeCurrentFragment(): void {
Expand Down
Loading