Cordova backbutton Event Never Firing

时间:2015-07-10 21:34:24

标签: javascript android cordova

根据我在backbutton事件后以[{1}}参数附加事件监听器false的文档覆盖我的设备后退按钮。

我这样做了:

deviceready

我疯狂地想弄清楚为什么点击设备按钮没有触发我附加的// Smart App object define([ 'routes', 'cordova', 'angular', 'angularRoute', 'angularResource', 'angularTouch', 'config', 'controllers', 'services', 'directives', 'helpers' ], function(appRoute) { var oApp = { _app: {}, init: function() { console.log('init'); document.addEventListener('deviceready', this.onDeviceReady, false); }, onDeviceReady: function() { console.log('ok device ready'); document.addEventListener('backbutton', function() { console.error('deviceback start'); angular.element('.ng-scope').scope().back(); console.error('deviceback end'); }, false); // ... } // ... 事件,我甚至在控制台中看不到backbutton消息。我现在在Android上测试我还没有测试任何其他手机操作系统。

console.error确实触发,我的应用正常工作,设备console.log('ok device ready')被覆盖,因为它的默认功能没有发生,但是如上所述,我的功能也没有发生。

我读了其他stackoverflow topcis并且他们说Cordova版本获得了修复,但我的Cordova版本远远落后于他们,我的backbutton文件告诉我:

.cordova/config.json

这是{ "lib": { "www": { "id": "com.tigoenergy.smart", "version": "3.5.0" } } } 然后我的3.5告诉我它是info.txt

5.0.0

有没有人有任何想法?

3 个答案:

答案 0 :(得分:1)

尝试使用正常使用cordova事件的方式。

// device APIs are available
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
    //Add your back button implementation.
}

官方文件here

修改后的代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Tigo SMART Installation</title>
<script type="text/javascript" src="apps/installation/js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="apps/installation/js/index.js"></script>
<script type="text/javascript" src="apps/installation/js/fastclick.min.js"></script>
<script type="text/javascript" src="apps/installation/js/sha512.js"></script>
<script type="text/javascript" src="bower_components/uri.js/src/URI.min.js"></script>
<script type="text/javascript" src="js/extlogin.js"></script>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" type="text/css" href="apps/installation/css/index.css" />

<script type="text/javascript" charset="utf-8">

$(document).ready(function(){
    //mycode
    console.log('ok loaded');
    document.addEventListener("deviceready", onDeviceReady, false);
});
// Wait for device API libraries to load
//
/*function onLoad() {
console.warn('ok loaded')
document.addEventListener("deviceready", onDeviceReady, false);
}
*/

// device APIs are available
//
function onDeviceReady() {
// Register the event listener
console.log('ok device ready');
document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
console.log('ok backey downed');
}

</script>
</head>
<body>

<div ng-view></div>
<script data-main="bin/SmartApp" src="bower_components/requirejs/require.min.js"></script>

<img style="opacity:0;visibility:hidden;" class="loader" src="img/loader.gif" />
</body>
</html>

很少有参考链接herehere

答案 1 :(得分:1)

问题是我在iframe中导入cordova.js,即使它已经在父窗口范围内。我不知道它会破坏它,并认为我必须在iframe中导入cordova.js。从iframe中删除修复它。

答案 2 :(得分:0)

试试这个

    $(document).ready(function () {

        document.addEventListener("backbutton", onBackKeyDown, false);
    });
    function onBackKeyDown() {
        alert('ok backey downed');
    }

确保这些文件是您网页中的链接

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>