问题描述
我正在开发一个HTML5网络应用程序,并用Cordova(phonegap)1.7编译它。
I am developing a HTML5 web-application and compiling it with Cordova (phonegap) 1.7.
我想重写Android的按钮,这样我可以调用window.history.back()而不是关闭应用程序(默认Android)。如何防止Android在按下后退按钮时杀死默认的活动?
I want to override the Android backbutton so that I can call window.history.back() instead of closing the application (default Android). How can I prevent Android from killing the defaultactivity on back button pressed?
我得到后退按钮按下!在logcat中,因此该方法在应用程序关闭之前触发。
I get the "Back button pressed!!!!" in logcat, so the method is fired before the application is closed.
这是我到目前为止:
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
document.addEventListener("backbutton", function(e) {
console.log("Back button pressed!!!!");
window.history.back();
}, false);
}
编辑:我愿意接受解释直接从DefaultActivity.java android类模拟window.history.back()如果可能的话!
I am willing to accept an answer explaining a way to simulate the window.history.back() directly from the DefaultActivity.java android class if that is possible!
推荐答案
我自己的问题通过添加下面的代码到 DefaultActivity.java
文件,以防止默认android行为,并保持JavaScript代码中的问题:
I solved my own question by adding the code below to the DefaultActivity.java
file to prevent the default android behavior, and keeping the JavaScript code as stated in the question:
@Override
public void onBackPressed() {
return;
}
我希望这有助于未来的同一个问题!
I hope this helps someone in the future with the same problem!
这篇关于如何防止Android在关闭web应用程序时按下backbutton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!