Skip to content

Commit 3f02413

Browse files
fix(android/application): open windows after the original activity finishes (#11552)
[skip ci]
1 parent 40c1682 commit 3f02413

3 files changed

Lines changed: 172 additions & 2 deletions

File tree

‎packages/core/application/application.android.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,20 @@ export class AndroidApplication extends ApplicationCommon implements IAndroidApp
792792

793793
const context = this.context ?? this.getNativeApplication();
794794
const intent = new android.content.Intent();
795-
const startActivity = this.startActivity;
795+
const windowActivities = this._getWindows()
796+
.filter((window) => window.state === 'attached' && !window._surfaceGone)
797+
.map((window) => (window as AndroidNativeWindow).activity);
798+
// startActivity/foregroundActivity can be third-party (SDK) activities, which must never be relaunched as a window.
799+
const startActivity = [this.startActivity, this.foregroundActivity, ...windowActivities].find((activity) => activity && 'isNativeScriptActivity' in activity && !activity.isFinishing() && !activity.isDestroyed());
796800

797801
if (startActivity) {
798802
intent.setClass(context, startActivity.getClass());
799803
} else {
800-
intent.setClassName(context, 'org.nativescript.NativeScriptActivity');
804+
const component = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName())?.getComponent();
805+
if (!component) {
806+
throw new Error('Unable to open a window: no NativeScript activity or launch activity was found.');
807+
}
808+
intent.setComponent(component);
801809
}
802810

803811
let flags = android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK | android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
vi.hoisted(() => {
4+
(globalThis as any).NativeScriptGlobals.addEventWiring = () => {};
5+
});
6+
7+
import { AndroidApplication } from './application.android';
8+
import type { AndroidNativeWindow } from '../native-window/native-window.android';
9+
10+
function activity(name: string, nativeScript = true) {
11+
return {
12+
...(nativeScript ? { isNativeScriptActivity: true } : {}),
13+
getClass: () => name,
14+
isFinishing: () => false,
15+
isDestroyed: () => false,
16+
isInMultiWindowMode: () => false,
17+
startActivity: vi.fn(),
18+
} as unknown as androidx.appcompat.app.AppCompatActivity;
19+
}
20+
21+
describe('Android openWindow activity selection', () => {
22+
let app: AndroidApplication;
23+
let context: any;
24+
let windows: AndroidNativeWindow[];
25+
let start: androidx.appcompat.app.AppCompatActivity;
26+
let foreground: androidx.appcompat.app.AppCompatActivity;
27+
let launchComponent: any;
28+
29+
beforeEach(() => {
30+
app = new AndroidApplication();
31+
start = undefined;
32+
foreground = undefined;
33+
windows = [];
34+
launchComponent = 'com.example.LaunchActivity';
35+
context = {
36+
startActivity: vi.fn(),
37+
getPackageName: () => 'com.example',
38+
getPackageManager: () => ({ getLaunchIntentForPackage: () => launchComponent && { getComponent: () => launchComponent } }),
39+
};
40+
vi.spyOn(console, 'warn').mockImplementation(() => {});
41+
vi.spyOn(app, 'context', 'get').mockReturnValue(context);
42+
vi.spyOn(app, 'startActivity', 'get').mockImplementation(() => start);
43+
vi.spyOn(app, 'foregroundActivity', 'get').mockImplementation(() => foreground);
44+
vi.spyOn(app, '_getWindows').mockImplementation(() => windows);
45+
});
46+
47+
afterEach(() => vi.restoreAllMocks());
48+
49+
function addWindow(owner: androidx.appcompat.app.AppCompatActivity, overrides = {}) {
50+
windows.push({ activity: owner, state: 'attached', _surfaceGone: false, ...overrides } as AndroidNativeWindow);
51+
}
52+
53+
function openedIntent() {
54+
const launchers = new Set([context, start, foreground, ...windows.map((window) => window.activity)]);
55+
const calls = [...launchers].filter(Boolean).flatMap((launcher) => vi.mocked(launcher.startActivity).mock.calls);
56+
expect(calls).toHaveLength(1);
57+
return calls[0][0];
58+
}
59+
60+
it('continues opening windows after the original activity has finished', () => {
61+
const surviving = activity('com.example.DocumentActivity');
62+
addWindow(surviving);
63+
64+
app.openWindow();
65+
66+
expect(openedIntent().component).toBe(surviving.getClass());
67+
});
68+
69+
it('keeps using the original NativeScript activity while it is available', () => {
70+
start = activity('com.example.MainActivity');
71+
foreground = activity('com.example.DocumentActivity');
72+
addWindow(foreground);
73+
74+
app.openWindow();
75+
76+
expect(openedIntent().component).toBe(start.getClass());
77+
});
78+
79+
it('uses a foreground NativeScript activity when the original is gone', () => {
80+
foreground = activity('com.example.DocumentActivity');
81+
82+
app.openWindow();
83+
84+
expect(openedIntent().component).toBe(foreground.getClass());
85+
});
86+
87+
it('does not reopen an SDK activity registered ahead of a surviving NativeScript window', () => {
88+
start = foreground = activity('com.example.PaymentActivity', false);
89+
addWindow(start);
90+
const surviving = activity('com.example.DocumentActivity');
91+
addWindow(surviving);
92+
93+
app.openWindow();
94+
95+
expect(openedIntent().component).toBe(surviving.getClass());
96+
});
97+
98+
it.each(['isFinishing', 'isDestroyed'] as const)('skips an activity whose %s is true', (method) => {
99+
start = activity('com.example.OldActivity');
100+
vi.spyOn(start, method).mockReturnValue(true);
101+
addWindow(start);
102+
const surviving = activity('com.example.DocumentActivity');
103+
addWindow(surviving);
104+
105+
app.openWindow();
106+
107+
expect(openedIntent().component).toBe(surviving.getClass());
108+
});
109+
110+
it.each([{ state: 'detached' }, { _surfaceGone: true }])('skips a window without a live surface: %j', (overrides) => {
111+
addWindow(activity('com.example.OldActivity'), overrides);
112+
const surviving = activity('com.example.DocumentActivity');
113+
addWindow(surviving);
114+
115+
app.openWindow();
116+
117+
expect(openedIntent().component).toBe(surviving.getClass());
118+
});
119+
120+
it('uses the configured launch component when there is no surviving NativeScript activity', () => {
121+
app.openWindow();
122+
123+
const intent = context.startActivity.mock.calls[0][0];
124+
expect(intent.component).toBe(launchComponent);
125+
expect(intent.flags).toBe(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK | android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
126+
});
127+
128+
it('reports a missing launch component without starting an undeclared activity', () => {
129+
launchComponent = undefined;
130+
131+
expect(() => app.openWindow()).toThrow(/no NativeScript activity or launch activity/i);
132+
expect(context.startActivity).not.toHaveBeenCalled();
133+
});
134+
});

‎packages/core/vitest.setup.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ global.__COMMONJS__ = false;
1818
global.__CSS_PARSER__ = 'css-tree';
1919
global.WeakRef.prototype.get = global.WeakRef.prototype.deref;
2020
global.NativeClass = function () {};
21+
global.android = {
22+
content: {
23+
Intent: class {
24+
static FLAG_ACTIVITY_NEW_TASK = 0x10000000;
25+
static FLAG_ACTIVITY_MULTIPLE_TASK = 0x08000000;
26+
static FLAG_ACTIVITY_NEW_DOCUMENT = 0x00080000;
27+
static FLAG_ACTIVITY_LAUNCH_ADJACENT = 0x00001000;
28+
component: any;
29+
flags = 0;
30+
extras = new Map<string, any>();
31+
setClass(context: any, activityClass: any) {
32+
this.component = activityClass;
33+
}
34+
setClassName(context: any, name: string) {
35+
this.component = name;
36+
}
37+
setComponent(component: any) {
38+
this.component = component;
39+
}
40+
setFlags(flags: number) {
41+
this.flags = flags;
42+
}
43+
putExtra(key: string, value: any) {
44+
this.extras.set(key, value);
45+
}
46+
},
47+
},
48+
};
2149
global.NSBundle = {
2250
mainBundle: {
2351
bundleIdentifier: 'test',

0 commit comments

Comments
 (0)