Skip to content

Commit f12000e

Browse files
authored
Merge pull request #1728 from m-abs/fix/923-module-not-destroyed
fix(NativeScriptPlatformRef): Destroy lastModuleRef on exitEvent
2 parents 20d48ee + ac9a59f commit f12000e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

nativescript-angular/platform-common.ts

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
on,
3434
launchEvent,
3535
LaunchEventData,
36+
exitEvent,
37+
ApplicationEventData,
3638
} from "tns-core-modules/application";
3739
import { TextView } from "tns-core-modules/ui/text-view";
3840

@@ -255,7 +257,29 @@ export class NativeScriptPlatformRef extends PlatformRef {
255257
args.root = rootContent;
256258
}
257259
);
260+
const exitCallback = profile(
261+
"nativescript-angular/platform-common.exitCallback", (args: ApplicationEventData) => {
262+
const androidActivity = args.android;
263+
if (androidActivity && !androidActivity.isFinishing()) {
264+
// Exit event was triggered as a part of a restart of the app.
265+
return;
266+
}
267+
268+
const lastModuleRef = lastBootstrappedModule ? lastBootstrappedModule.get() : null;
269+
if (lastModuleRef) {
270+
// Make sure the module is only destroyed once
271+
lastBootstrappedModule = null;
272+
273+
lastModuleRef.destroy();
274+
}
275+
276+
if (!autoCreateFrame) {
277+
rootContent = null;
278+
}
279+
}
280+
);
258281
on(launchEvent, launchCallback);
282+
on(exitEvent, exitCallback);
259283

260284
applicationRun();
261285
}

nativescript-angular/router/page-router-outlet.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,19 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
352352
// Add it to the new page
353353
page.content = componentView;
354354

355-
page.on(Page.navigatedFromEvent, (<any>global).Zone.current.wrap((args: NavigatedData) => {
355+
const navigatedFromCallback = (<any>global).Zone.current.wrap((args: NavigatedData) => {
356356
if (args.isBackNavigation) {
357357
this.locationStrategy._beginBackPageNavigation(this.frame);
358358
this.locationStrategy.back(null, this.frame);
359359
}
360-
}));
360+
});
361+
page.on(Page.navigatedFromEvent, navigatedFromCallback);
362+
componentRef.onDestroy(() => {
363+
if (page) {
364+
page.off(Page.navigatedFromEvent, navigatedFromCallback);
365+
page = null;
366+
}
367+
});
361368

362369
const navOptions = this.locationStrategy._beginPageNavigation(this.frame);
363370

@@ -367,14 +374,15 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
367374
if (this.outlet) {
368375
this.routeReuseStrategy.clearCache(this.outlet.outletKeys[0]);
369376
}
370-
page.off(Page.navigatedToEvent, clearCallback);
371377
});
372378

373-
page.on(Page.navigatedToEvent, clearCallback);
379+
page.once(Page.navigatedToEvent, clearCallback);
374380
}
375381

376382
this.frame.navigate({
377-
create: () => { return page; },
383+
create() {
384+
return page;
385+
},
378386
clearHistory: navOptions.clearHistory,
379387
animated: navOptions.animated,
380388
transition: navOptions.transition

0 commit comments

Comments
 (0)