科尔多瓦后退按钮触发事件监听器,但应用仍然关闭 [英] cordova back button fires event listener but app closes anyway

查看:73
本文介绍了科尔多瓦后退按钮触发事件监听器,但应用仍然关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,如果用户按下设备上的后退按钮,我想向他显示一个弹出窗口,然后他才能退出该应用程序.但是,这对我不起作用.显示了警报框,但应用程序也关闭了.

I have the following code where if the user presses back button on his device, I want to show him a popup before he is able to exit the app. However, this does not work for me. The alert box is showed but the app also closes.

document.addEventListener("deviceready", function() {
    document.addEventListener("backbutton", function(e) {
        e.preventDefault();
        $scope.alertDialog.show();
    }, false);
}, false);

科尔多瓦版本:6.4.0 并且在有人提出之前-cordova.js已包含在索引html页面中. UI是使用onsenUI和angularJS v1构建的.

Cordova version:6.4.0 And before someone brings it out - cordova.js is included in the index html page. UI is built using onsenUI with angularJS v1.

推荐答案

弄清楚了. 如何控制android backbutton路由? 最后0个投票答案是正确的答案.

Figured it out. How to control android backbutton routes? The last 0 voted answer is the right one.

您可以在之后使用"disableDeviceBackButtonHandler"对其进行控制 准备就绪事件.之后,为后退按钮添加一个事件侦听器,并 做任何你想做的事.

You can controll it with "disableDeviceBackButtonHandler" after ons.ready event. After that add a event listener for back button and do anything you want.

ons.ready(function() {
  ons.disableDeviceBackButtonHandler();

  // Use Cordova handler
  window.document.addEventListener('backbutton', function() {
    // Handle backbutton event
  }, false);
});

这篇关于科尔多瓦后退按钮触发事件监听器,但应用仍然关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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