diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index 2f7e958cd..d6a4c557f 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -33,6 +33,8 @@ import { on, launchEvent, LaunchEventData, + exitEvent, + ApplicationEventData, } from "tns-core-modules/application"; import { TextView } from "tns-core-modules/ui/text-view"; @@ -255,7 +257,29 @@ export class NativeScriptPlatformRef extends PlatformRef { args.root = rootContent; } ); + const exitCallback = profile( + "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(); + } + + if (!autoCreateFrame) { + rootContent = null; + } + } + ); 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 fdcd5e2b1..63f9ea4a8 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); @@ -367,14 +374,15 @@ 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({ - create: () => { return page; }, + create() { + return page; + }, clearHistory: navOptions.clearHistory, animated: navOptions.animated, transition: navOptions.transition