我正在使用 phonegap-1.3.0 和 android-4.0.3 开发一个应用程序。下面是我的代码:
function home() {
document.removeEventListener("backbutton", handleBackButton, false);
document.addEventListener("backbutton", handleBackButtonOnHome, false);
}
function edit() {
document.removeEventListener("backbutton", handleBackButtonOnHome, false); document.addEventListener("backbutton", handleBackButton, false);
}
function handleBackButton() {
console.log("Back Button Pressed!");
home();
}
function handleBackButtonOnHome() {
console.log("Back Button Pressed in home!");
navigator.app.exitApp();
}
单击编辑页面上的硬件后退按钮会将用户带到主页,当在主页上时,应用程序将按照事件处理程序的指定退出。该应用程序在指定的设置(配置)上运行良好。
最近我升级到 cordova-2.1.0,点击编辑页面上的后退按钮退出应用程序而不是将用户带到主页。
请注意:我已经试过了,但似乎没有任何效果,
navigator.app.backHistory()
history.back()
欢迎任何帮助..
最佳答案
在我的环境(PhoneGap 中的 Sencha Touch 2,请参阅 Andreas Sommer 的说明 here)中寻找此问题的解决方案时,我通过将以下代码添加到我的 index.html HEAD 部分来修复:
<!-- handle android hardware back button -->
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", function() {
document.addEventListener("backbutton", function() {
if (Ext.getCmp('mainview').pop(1) == null) {
Ext.Msg.confirm("Exit", "Do you want to Exit?", function(e) {
if (e == 'yes') {
navigator.app.exitApp();
}
});
}
else {
return false;
}
}, false);
}, false);
</script>
仅供引用,“主视图”组件是一个 Ext.navigation.View,我从堆栈中弹出当前 View 。如果 pop() 返回 null,那么我们就在主视图中。
这适用于 Gingerbread 和 ICS,但不适用于 Jelly Bean。为了让它与 Jelly Bean 一起工作,我需要从 PhoneGap 下的 AndroidManifest.xml 文件中的标签中删除 android:targetSdkVersion="17"属性。
所有这些只适用于 PhoneGap。当我在 Sencha Touch 而不是 PhoneGap 下生成 .apk 时,硬件后退按钮没有被捕获。
关于android - Phonegap-2.10 : Hardware back button not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837649/