From a21454f1e1b9f823dfa71c8f6a8fc9f1ff2174c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Tue, 12 Feb 2019 14:19:28 +0100 Subject: [PATCH 1/6] chor(NativeScriptPlatformRef): Destroy lastModuleRef on exitEvent --- nativescript-angular/platform-common.ts | 10 ++++++++++ nativescript-angular/router/page-router-outlet.ts | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 2f7e958cd..4eb5d7900 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -33,6 +33,7 @@ import { on, launchEvent, LaunchEventData, + exitEvent, } from "tns-core-modules/application"; import { TextView } from "tns-core-modules/ui/text-view"; @@ -255,7 +256,16 @@ export class NativeScriptPlatformRef extends PlatformRef { args.root = rootContent; } ); + const exitCallback = profile( + "nativescript-angular/platform-common.exitCallback",() => { + const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null; + if (lastModuleRef) { + lastModuleRef.destroy(); + } + } + ); on(launchEvent, launchCallback); + on(exitEvent, exitCallback); applicationRun(); } diff --git a/nativescript-angular/router/page-router-outlet.ts b/nativescript-angular/router/page-router-outlet.ts index 381ff52bd..6ad425c65 100644 --- a/nativescript-angular/router/page-router-outlet.ts +++ b/nativescript-angular/router/page-router-outlet.ts @@ -352,12 +352,19 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire // Add it to the new page page.content = componentView; - page.on(Page.navigatedFromEvent, (global).Zone.current.wrap((args: NavigatedData) => { + const navigatedFromCallback = (global).Zone.current.wrap((args: NavigatedData) => { if (args.isBackNavigation) { this.locationStrategy._beginBackPageNavigation(this.frame); this.locationStrategy.back(null, this.frame); } - })); + }); + page.on(Page.navigatedFromEvent, navigatedFromCallback); + componentRef.onDestroy(() => { + if (page) { + page.off(Page.navigatedFromEvent, navigatedFromCallback); + page = null; + } + }); const navOptions = this.locationStrategy._beginPageNavigation(this.frame); @@ -374,7 +381,9 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire } this.frame.navigate({ - create: () => { return page; }, + create() { + return page; + }, clearHistory: navOptions.clearHistory, animated: navOptions.animated, transition: navOptions.transition From a4ee021fb0f3b74cf2ee3b0b9ec5ca06e8b1ec61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Tue, 12 Feb 2019 14:34:11 +0100 Subject: [PATCH 2/6] fix(tslint): missing whitespace --- nativescript-angular/platform-common.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 4eb5d7900..12958d1ae 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -257,7 +257,7 @@ export class NativeScriptPlatformRef extends PlatformRef { } ); const exitCallback = profile( - "nativescript-angular/platform-common.exitCallback",() => { + "nativescript-angular/platform-common.exitCallback", () => { const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null; if (lastModuleRef) { lastModuleRef.destroy(); From 14e787fb160d2a6f8e8ebbbaff88e745eee8ac1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Thu, 21 Feb 2019 14:26:00 +0100 Subject: [PATCH 3/6] fix: remove rootContent on exit --- nativescript-angular/platform-common.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 12958d1ae..2ef914c98 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -262,6 +262,10 @@ export class NativeScriptPlatformRef extends PlatformRef { if (lastModuleRef) { lastModuleRef.destroy(); } + + if (!autoCreateFrame) { + rootContent = null; + } } ); on(launchEvent, launchCallback); From ea66985a9cc94a69fc40d7d8731b4a2f1262b8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Fri, 22 Feb 2019 11:40:38 +0100 Subject: [PATCH 4/6] fix: page might be null'ed before clearHistory's navigatedToEvent --- nativescript-angular/router/page-router-outlet.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nativescript-angular/router/page-router-outlet.ts b/nativescript-angular/router/page-router-outlet.ts index 6ad425c65..cc9d07cc2 100644 --- a/nativescript-angular/router/page-router-outlet.ts +++ b/nativescript-angular/router/page-router-outlet.ts @@ -374,10 +374,9 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire if (this.outlet) { this.routeReuseStrategy.clearCache(this.outlet.outletKeys[0]); } - page.off(Page.navigatedToEvent, clearCallback); }); - page.on(Page.navigatedToEvent, clearCallback); + page.once(Page.navigatedToEvent, clearCallback); } this.frame.navigate({ From 5e13263389ae0c97cd4f92693a11ba38884fc5a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Fri, 8 Mar 2019 15:39:51 +0100 Subject: [PATCH 5/6] fix: on destroy remove the lastBootstrappedModule If exit event was triggered twice we would try to destroy an already destroy module ref. --- nativescript-angular/platform-common.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 2ef914c98..8c1c342b6 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -260,6 +260,8 @@ export class NativeScriptPlatformRef extends PlatformRef { "nativescript-angular/platform-common.exitCallback", () => { const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null; if (lastModuleRef) { + lastBootstrappedModule = null; + lastModuleRef.destroy(); } From 3dffbd512f31856c48dc9f9dcea754f0bca7a03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Anton=20Bach=20Sj=C3=B8gren?= Date: Mon, 11 Mar 2019 13:41:23 +0100 Subject: [PATCH 6/6] fix: the exit event is triggered on restart Don't destroy the angular module on restart --- nativescript-angular/platform-common.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 8c1c342b6..d6a4c557f 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -34,6 +34,7 @@ import { launchEvent, LaunchEventData, exitEvent, + ApplicationEventData, } from "tns-core-modules/application"; import { TextView } from "tns-core-modules/ui/text-view"; @@ -257,9 +258,16 @@ export class NativeScriptPlatformRef extends PlatformRef { } ); const exitCallback = profile( - "nativescript-angular/platform-common.exitCallback", () => { + "nativescript-angular/platform-common.exitCallback", (args: ApplicationEventData) => { + const androidActivity = args.android; + if (androidActivity && !androidActivity.isFinishing()) { + // Exit event was triggered as a part of a restart of the app. + return; + } + const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null; if (lastModuleRef) { + // Make sure the module is only destroyed once lastBootstrappedModule = null; lastModuleRef.destroy();