Content you want the user to see goes here.

Senin, 28 Agustus 2017

Ionic 2 Hard Back Button

import { App, AlertController, Nav, Platform } from 'ionic-angular';

export class blabla {
@ViewChild(Nav) nav: Nav;
private rootPageToExitOn: string = "TabsPage";
 alertShow: boolean = false;

--- in whatever function (It's fine in initializeApp())
this.platform.ready().then(() => {
this.platform.registerBackButtonAction(() => {
let nav = this.app.getActiveNav();
if(nav.canGoBack()) {
nav.pop();
}
else {
let activePage = nav.getActive().instance;
if(this.rootPageToExitOn === activePage.constructor.name) {
           if(this.alertShow === false) {
           this.alertShow = true;
let alert = this.alertCtrl.create({
title: '',
message: 'Exit?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
                    this.alertShow = false;
                  }
},
               {
text: 'Exit',
handler: () => {
this.platform.exitApp();
}
}
]
});
alert.present();
           }
}
else {
nav.setRoot(TabsPage);
}
}
});
}
}