Ionic2 App Exit Event

Source: Internet
Author: User
Tags export class
 Import {Component, viewchild} from ' @angular/core ';
 Import {Platform, Toastcontroller, Nav, Ionicapp} from ' Ionic-angular ';
 Import {StatusBar, splashscreen} from ' ionic-native '; Import {tabspage} from '.

 /pages/tabs/tabs ';
   @Component ({templateurl: ' app.html '}) Export class MyApp {rootpage = tabspage;  Backbuttonpressed:boolean = false;

   Used to determine whether the return key triggers a @ViewChild (' Mynav ') Nav:nav; Constructor (public Ionicapp:ionicapp, public platform:platform, public Toastctrl:toastcontroller) {Platform.ready
       (). Then (() = {Statusbar.styledefault ();
       Splashscreen.hide ();
   This.registerbackbuttonaction ();//Register Return key event}); } registerbackbuttonaction () {this.platform.registerBackButtonAction () = {//If you want to click the Back button to hide toast or loadin G or overlay Add the following//This.ionicapp._toastportal.getactive () | | This.ionicapp._loadingportal.getactive () | | This.ionicapp._overlayportal.getactive () Let activeportal = This.ionicapp._modalportaL.getactive ();
         if (activeportal) {Activeportal.dismiss (). catch (() = {});
         Activeportal.ondiddismiss (() = {});
       Return
       } Let Activevc = This.nav.getActive ();
       let tabs = ActiveVC.instance.tabs;
       Let Activenav = tabs.getselected (); Return Activenav.cangoback ()?
   Activenav.pop (): This.showexit ()}, 1); }//Double exit cue box Showexit () {if (this.backbuttonpressed) {//When the trigger flag is true, that is, 2 seconds to double-click the Back button to exit the app This.platform.exi
     TAPP ();
       } else {this.toastCtrl.create ({message: ' Press again to exit the app ', duration:2000, Position: ' Top '
       }). present ();
       This.backbuttonpressed = true; SetTimeout (() = This.backbuttonpressed = False, 2000);//2 seconds without a second click Back to mark the trigger flag as false}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.