2016-12-14 12 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(); 
} 
関連する問題