![](/img/trans.png)
[英]How to get a back button in google maps in Phonegap/Cordova (iOS & Android)
[英]How to detect Android back button in phonegap/cordova
我目前正在使用cordova 3.7.1。 在我的應用程序中,我無法在jquery腳本中檢測到硬件后退按鈕。 我嘗試這樣: $(document).ready(function() { //registering the back button document.addEventListener("backbutton", onBackKeyDown, false); });
function onBackKeyDown(e) { alert("back button pressed");//alert if the android back button is pressed }
但這不起作用。 我已經嘗試了所有可能性
我還嘗試使用MainActivity.java中的當前URL
appView.getUrl();
但這不會返回div的url如果我有一個div作為#page2它沒有返回url。
它只返回http://sas.cer.org/index.html 。 它沒有返回http://sas.cer.org/index.html#page2
我也在使用jquery mobile。
是否有任何替代方法來處理Native或Jquery端的android / hardware后退按鈕?
以下內容適用於我們部署到cordova和web的應用程序。
如果在cordova中,則需要window._cordovaNative = true
。
我在那里留下了我的按鈕處理代碼(參見“ // close menu if open
”和其他注釋),您需要用代碼替換它們。
把它放在某個地方:
let bNH, bakNavHandler = bNH = {
warningOpen: false,
init: function(){
if (window._cordovaNative)
document.addEventListener('backbutton', this.onback, false);
else {
if (!window.performance || performance.navigation.type != 1)
this.preventDefault();
window.onpopstate = this.onback;
}
},
preventDefault: function(e){
window._cordovaNative ?
e.preventDefault() :
window.history.pushState(null, '', window.location.href);
},
onback: function(e){
// close menu if open
if ($('#ekapp_menu_div').css('margin-right') == '-2px'){
bNH.preventDefault(e)
_that.hideMenuDiv();
}
// close modal if open
else if (!bNH.warningOpen && $('#ekapp_modal:visible')[0]){
bNH.preventDefault(e)
_that.closeModal();
}
// prev screen if history
else if (_that.history.length > 1) {
bNH.preventDefault(e)
_that.previousScreen();
}
// show close app warning
else if (!bNH.warningOpen) {
if (window._cordovaNative)
bNH.preventDefault(e);
_that.openModal('Tap back button again to exit app!');
bNH.warningOpen = true;
$('#ekapp_modal_buttons .ekapp_cancel_btn').one('click', function(){
bNH.warningOpen = false;
if (!window._cordovaNative)
bNH.preventDefault();
});
}
}
};
然后在你的deviceready(cordova)或doc ready(web)init函數中執行:
bakNavHandler.init();
閱讀文檔,你有一個完整的例子
您必須偵聽deviceready事件,而不是文檔就緒
<!DOCTYPE html>
<html>
<head>
<title>Back Button Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.