Skip to content

Commit 8f0b88b

Browse files
committed
fix(NavController): fire onPageWillUnload/DidUnload
Fixes #5507
1 parent 7e3eb23 commit 8f0b88b

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

ionic/components/nav/nav-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ export class NavController extends Ion {
802802
// set that it is the init leaving view
803803
// the first view to be removed, it should init leave
804804
view.state = STATE_INIT_LEAVE;
805+
view.willUnload();
805806

806807
// from the index of the leaving view, go backwards and
807808
// find the first view that is inactive so it can be the entering
@@ -836,7 +837,6 @@ export class NavController extends Ion {
836837
this._views.filter(v => v.state === STATE_REMOVE).forEach(view => {
837838
view.willLeave();
838839
view.didLeave();
839-
view.didUnload();
840840
this._views.splice(this.indexOf(view), 1);
841841
view.destroy();
842842
});

ionic/components/nav/test/basic/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ class AnotherPage {
271271
constructor(
272272
private nav: NavController,
273273
private viewCtrl: ViewController
274-
) {}
274+
) {
275+
console.log('Page, AnotherPage, constructor', this.viewCtrl.id);
276+
}
275277

276278
pushFullPage() {
277279
this.nav.push(FullPage);
@@ -304,6 +306,34 @@ class AnotherPage {
304306
this.viewCtrl.setBackButtonText(backButtonText);
305307
++this.bbCount;
306308
}
309+
310+
onPageWillEnter() {
311+
console.log('Page, AnotherPage, onPageWillEnter', this.viewCtrl.id);
312+
}
313+
314+
onPageDidEnter() {
315+
console.log('Page, AnotherPage, onPageDidEnter', this.viewCtrl.id);
316+
}
317+
318+
onPageWillLeave() {
319+
console.log('Page, AnotherPage, onPageWillLeave', this.viewCtrl.id);
320+
}
321+
322+
onPageDidLeave() {
323+
console.log('Page, AnotherPage, onPageDidLeave', this.viewCtrl.id);
324+
}
325+
326+
onPageWillUnload() {
327+
console.log('Page, AnotherPage, onPageWillUnload', this.viewCtrl.id);
328+
}
329+
330+
onPageDidUnload() {
331+
console.log('Page, AnotherPage, onPageDidUnload', this.viewCtrl.id);
332+
}
333+
334+
ngOnDestroy() {
335+
console.log('Page, AnotherPage, ngOnDestroy', this.viewCtrl.id);
336+
}
307337
}
308338

309339

ionic/components/nav/view-controller.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,6 @@ export class ViewController {
183183
return (this.index === 0);
184184
}
185185

186-
/**
187-
* @private
188-
*/
189-
addDestroy(destroyFn: Function) {
190-
this._destroys.push(destroyFn);
191-
}
192-
193-
/**
194-
* @private
195-
*/
196-
destroy() {
197-
for (var i = 0; i < this._destroys.length; i++) {
198-
this._destroys[i]();
199-
}
200-
this._destroys = [];
201-
}
202-
203186
/**
204187
* @private
205188
*/
@@ -489,10 +472,21 @@ export class ViewController {
489472

490473
/**
491474
* @private
492-
* The view has been destroyed and its elements have been removed.
493475
*/
494-
didUnload() {
476+
addDestroy(destroyFn: Function) {
477+
this._destroys.push(destroyFn);
478+
}
479+
480+
/**
481+
* @private
482+
*/
483+
destroy() {
495484
ctrlFn(this, 'onPageDidUnload');
485+
486+
for (var i = 0; i < this._destroys.length; i++) {
487+
this._destroys[i]();
488+
}
489+
this._destroys = [];
496490
}
497491

498492
}

0 commit comments

Comments
 (0)