在 Ionic 应用程序中禁用硬件后退按钮? [英] Disable hardware back button in Ionic application?

查看:36
本文介绍了在 Ionic 应用程序中禁用硬件后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试禁用我的 Cordova 应用程序上的后退按钮.我正在使用 AngularJS + Ionic 框架.我找到了关于这个的主题并尝试了下面的代码,但它绝对没有效果.有什么想法吗?

I'm trying to disable the back button on my Cordova app. I'm using AngularJS + Ionic Framework. I found topics about this and tried the code bellow, but it has absolutely no effect. Any idea?

index.html

<head>
    <script>
      document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            document.addEventListener("backbutton", function (e) {
                e.preventDefault();
                console.log("hello");
            }, false );
        }
    </script>
</head>

请注意,当我按下后退按钮时,控制台中会显示hello".

Note that when I push back button, I have "hello" displayed in my console.

推荐答案

终于在this 离子论坛主题:

Finally found the answer on this Ionic Forum thread:

$ionicPlatform.registerBackButtonAction(function () {
  if (condition) {
    navigator.app.exitApp();
  } else {
    handle back action!
  }
}, 100);

$ionicPlatform.registerBackButtonAction 允许完全覆盖后退按钮行为.第一个参数是回调函数,第二个参数是优先级(只有优先级最高的回调才会被执行).

$ionicPlatform.registerBackButtonAction allows to completly overwrite back button behavior. First param is a callback function and the secondone a priority (only the callback with the highest priority gets executed).

这篇关于在 Ionic 应用程序中禁用硬件后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆