Device backbutton (platform.registerBackButtonAction) is not working. it always exit from the application

Multi tool use
Multi tool use


Device backbutton (platform.registerBackButtonAction) is not working. it always exit from the application



Device backbutton (platform.registerBackButtonAction) is not working for Application back button and it's not working for Device back button.



Can some one help me please?



How can I solve my problem?


ionViewDidLoad() {
this.navBar.backButtonClick = (e: UIEvent) => {
const alert = this.alertCtrl.create({
title: 'App termination',
message: 'Do you want to close the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Application exit prevented!');
}
}, {
text: 'Close App',
handler: () => {
this.platform.exitApp(); // Close this application
}
}]
});
alert.present();
}
}




1 Answer
1



Modify your app.component.ts file in following way,


import { Platform, IonicApp } from 'ionic-angular';

constructor(public platform: Platform, private ionicApp: IonicApp){}

initializeApp() {
this.platform.ready().then(() => {
//back button handle
this.platform.registerBackButtonAction(() => {
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._toastPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();

if (activePortal) {
activePortal.dismiss();
}
else {
if (this.nav.canGoBack()) {
this.nav.pop();
} else {
this.showAlert();
}
}
});
});
}

showAlert() {
let confirm = this.alertCtrl.create({
title: 'Exit Application?',
message: 'Do you want to exit this application?',
buttons: [
{
text: 'No',
handler: () => {}
},
{
text: 'Yes',
handler: () => {
navigator['app'].exitApp();
}
}
]
});
confirm.present();
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

RMtb4QmMBH8PE,h8,VZOIq0bAzZJ0KX,sqnkKd0,lHsqSd
jCb9LZ8XScuM

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications