Ionic对返回键的处理

ionic做了一个tabs的程序但是在安卓机上测试发现只要一按返回键程序就退出了再按程序就会重新加载这样的交互效果非常的不友好于是查找文档找到解决方法当按返回键时弹出个对话框提示用户选择退出才真的退出程序否则按返回键则不进行任何操作

只需要更改app.components.ts文件具体的代码如下所示

 

export class MyApp {
  rootPage:any = HomePage;
    //程序默认在platform: Platform前没加public,记得加哦
  constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
    public alertCtrl: AlertController) {
    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();
 
        //注册返回按钮的处理方法
        platform.registerBackButtonAction(() => {
            this.showPrompt();
           return false;
        });
    });
  }
 
   //退出提示框,按确定按钮才退出
   showPrompt() {
    let prompt = this.alertCtrl.create({
      title: '确认退出吗?',
      message: "感谢您对本软件的支持!",
      buttons: [
        {
          text: '取消',
          handler: data => {
          }
        },
        {
          text: '退出',
          handler: data => {
            this.platform.exitApp();
          }
        }
      ]
    });
    prompt.present();//弹出提示框
  }
}

主要是要调用platformregisterBackButtonAction方法对其进行处理

 

如果觉得写的不错可以打赏哦您的支持是我一直努力的动力

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值