Alert Controller for Exit application

import { Component,ViewChild } from '@angular/core';
import { Nav, Platform, AlertController  } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { MenuPage } from "../pages/menu/menu";
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  rootPage:any = MenuPage;
  alert: any;
  constructor(public platform: Platform,public statusBar: StatusBar, public splashScreen: SplashScreen,  public alertCtrl:  AlertController) {
    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.
      this.statusBar.backgroundColorByHexString('#161f27');
      this.splashScreen.hide();
      this.platform.registerBackButtonAction(() =>{

      if(this.nav.canGoBack()){
        this.nav.pop();
      }else{
        if(this.alert){
          this.alert.dismiss();
          this.alert =null;   
        }else{
          this.showAlert();
        }
      }
    });
   
    });
  }
  showAlert() {
    this.alert = this.alertCtrl.create({
      title: 'Exit ?',
      message: 'Are you sure to exit ?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            this.alert =null;
          }
        },
        {
          text: 'Exit',
          handler: () => {
            this.platform.exitApp();
          }
        }
      ]
    });
    this.alert.present();
  }
}

Comments

Popular posts from this blog

Hide Tabs when open sub pages