ionic2 android 版本,ionic2实战-完美处理安卓硬件返回按钮

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; //用于判断返回键是否触发

@ViewChild('myNav') nav: Nav;

constructor(public ionicApp: IonicApp, public platform: Platform, public toastCtrl: ToastController) {

platform.ready().then(() => {

StatusBar.styleDefault();

Splashscreen.hide();

this.registerBackButtonAction();//注册返回按键事件

});

}

registerBackButtonAction() {

this.platform.registerBackButtonAction(() => {

//如果想点击返回按钮隐藏toast或loading或Overlay就把下面加上

// 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);

}

//双击退出提示框

showExit() {

if (this.backButtonPressed) { //当触发标志为true时,即2秒内双击返回按键则退出APP

this.platform.exitApp();

} else {

this.toastCtrl.create({

message: '再按一次退出应用',

duration: 2000,

position: 'top'

}).present();

this.backButtonPressed = true;

setTimeout(() => this.backButtonPressed = false, 2000);//2秒内没有再次点击返回则将触发标志标记为false

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值