Skip to content

Commit 6b0790f

Browse files
anjak03edusperoni
andauthored
fix(core): fire launch again for the first window after exit (#11573)
Both platforms now raise exit through an internal notifyExit(), which resets the launch bridge before notifying. This keeps the flag's lifecycle next to notifyLaunch() and stops Application.off('exit') or a manually notified exit from affecting it. --------- Co-authored-by: Eduardo Speroni <edusperoni@gmail.com>
1 parent e4c2644 commit 6b0790f

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎packages/core/application/application-common.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ export class ApplicationCommon {
154154
* so it also fires on a background launch where no window is created.
155155
*
156156
* Guaranteed ordering: `ready` -> `windowOpen` -> raw connect/create events -> content
157-
* resolution (the legacy `launch` bridge runs here, for the first window only) ->
157+
* resolution (the legacy `launch` bridge runs here, for the first window and the first
158+
* one after `exit`) ->
158159
* `contentLoaded` -> `activate`/`displayed`.
159160
*/
160161
readonly readyEvent = 'ready';
@@ -732,12 +733,26 @@ export class ApplicationCommon {
732733
});
733734
}
734735

736+
/**
737+
* @internal - raises `exit` and re-arms the legacy `launch` bridge, since the process
738+
* can outlive the exit and open a new first window.
739+
*/
740+
notifyExit(data?: Partial<ApplicationEventData>): void {
741+
this._launchBridgeConsumed = false;
742+
743+
this.notify(<ApplicationEventData>{
744+
eventName: this.exitEvent,
745+
object: this,
746+
...data,
747+
});
748+
}
749+
735750
/**
736751
* @internal - produces the content for a window that has none.
737752
*
738753
* Resolution order: the window content resolver, then the legacy `launch` event
739-
* (offered to the first window that asks for content and to no other), then the
740-
* application main entry. A resolver or a `launch` handler returning `null` takes
754+
* (offered to the first window that asks for content, and again to the first one after
755+
* `exit`), then the application main entry. A resolver or a `launch` handler returning `null` takes
741756
* ownership of the content, so nothing else is tried. A missing main entry leaves
742757
* the window empty instead of throwing, because content can still arrive later
743758
* through `run()`/`resetRootView()`.

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,7 @@ function initNativeScriptLifecycleCallbacks() {
188188
// This callback runs for every activity in the process, not just NativeScript ones,
189189
// so an empty registry here means the app really has no window left.
190190
if (activity.isFinishing() && Application.android._getWindows().length === 0) {
191-
Application.android.notify({
192-
eventName: Application.exitEvent,
193-
object: Application.android,
194-
android: activity,
195-
});
191+
Application.android.notifyExit({ android: activity });
196192
}
197193

198194
// TODO: This is a temporary workaround to force the V8's Garbage Collector, which will force the related Java Object to be collected.

‎packages/core/application/application.ios.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,11 +1369,7 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
13691369
}
13701370

13711371
private willTerminate(notification: NSNotification) {
1372-
this.notify(<ApplicationEventData>{
1373-
eventName: this.exitEvent,
1374-
object: this,
1375-
ios: this.ios,
1376-
});
1372+
this.notifyExit({ ios: this.ios });
13771373

13781374
// const rootView = this._rootView;
13791375
// if (rootView && rootView.isLoaded) {

‎packages/core/application/window-content-resolver.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ describe('window content resolution', () => {
270270
expect(launched).toHaveBeenCalledTimes(1);
271271
});
272272

273+
it('fires again for the next window after the app exits', () => {
274+
const launched = vi.fn();
275+
app.on('launch', launched);
276+
const reopened = new TestWindow('window-2', true);
277+
278+
app._resolveWindowContent(window, request);
279+
app.notifyExit();
280+
app._resolveWindowContent(reopened, { window: reopened, isPrimary: true });
281+
282+
expect(launched).toHaveBeenCalledTimes(2);
283+
expect(reopened.rootView).toBe(mainEntryView);
284+
});
285+
273286
it('never fires once a resolver has answered the first window', () => {
274287
const launched = vi.fn();
275288
app.on('launch', launched);

0 commit comments

Comments
 (0)