2016-12-14 147 views

回答

5
export class MyApp{ 
    constructor(public alert: AlertController,public platform: Platform){} 
    exit(){ 
     let alert = this.alert.create({ 
     title: 'Confirm', 
     message: 'Do you want to exit?', 
     buttons: [{ 
      text: "exit?", 
      handler:() => { this.exitApp() } 
     }, { 
      text: "Cancel", 
      role: 'cancel' 
     }] 
     }) 
     alert.present(); 
    } 
    exitApp(){ 
    this.platform.exitApp(); 
    } 
} 

如果您想啓用後退按鈕退出,請爲其添加事件監聽器並調用exit函數。

您可以使用this.platform.registerBackButtonAction(this.exit)

+1

退出偶數頁推應用程序,它不應該退出,如果任何頁面打開 – rashidnk

1

我可以通過自己找到合適的解決方案:

https://forum.ionicframework.com/t/show-a-confirmation-alert-before-app-close-ionic/63313

showedAlert: boolean; 

constructor(..., public alertCtrl: AlertController) { 
} 

initializeApp() { 
    this.platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
     this.showedAlert = false; 

     // Confirm exit 
     this.platform.registerBackButtonAction(() => { 
      if (this.nav.length() == 1) { 
       if (!this.showedAlert) { 
        this.confirmExitApp(); 
       } else { 
        this.showedAlert = false; 
        this.confirmAlert.dismiss(); 
       } 
      } 

      this.nav.pop(); 
     }); 

    }); 
} 

confirmExitApp() { 
    this.showedAlert = true; 
    this.confirmAlert = this.alertCtrl.create({ 
     title: "Salir", 
     message: "¿ Esta seguro que desea salir de la aplicación ?", 
     buttons: [ 
      { 
       text: 'Cancelar', 
       handler:() => { 
        this.showedAlert = false; 
        return; 
       } 
      }, 
      { 
       text: 'Aceptar', 
       handler:() => { 
        this.platform.exitApp(); 
       } 
      } 
     ] 
    }); 
    this.confirmAlert.present(); 
} 
相關問題