破坏后退按钮事件- android

Destroy back button event- android

本文关键字:事件 android 按钮      更新时间:2023-09-26

我为Android的后退按钮(PhoneGap 2.2.0)创建了以下事件:

document.addEventListener("backbutton", function (e) {
    //Do Something
}, false);

我有以下链接,将我从应用程序到外部站点

我的链接

我重写了后退按钮事件,一旦我在应用程序内部传递到另一个链接,该事件就不会被取消。由于另一个链接不知道cordova,他甚至不能访问这个事件。

所以我必须完全取消它!

我是怎么做的?

当我按下后退按钮时,我在日志中得到以下错误信息:

Uncaught ReferenceError: cordova is not defined at :1
function onBackKey() {
    console.log("I've caught a back key");
    // We are going back to home so remove the event listener
    // so the default back key behaviour will take over
    document.removeEventListener("backbutton", onBackKey, false);
    // Hide the current dive and show home
    document.getElementById(cur).style.display = 'none';
    document.getElementById('home').style.display = 'block';    
    cur = 'home';
}
function goToDiv(id) {
    // We are moving to a new div so over ride the back button
    // so when a users presses back it will show the home div
    document.addEventListener("backbutton", onBackKey, false);
    // Hide home and show the new div
    document.getElementById('home').style.display = 'none';
    document.getElementById(id).style.display = 'block';
    cur = id;
}

放置HTML标签

 <div id="home">Back Button Home<br/><a href="javascript:goToDiv('div1')">Div One</a><br/><a href="javascript:goToDiv('div2')">Div Two</a></div>

请在下面找到详细答案的链接

https://gist.github.com/955496

您可以通过

boolean toRun = true;
document.addEventListener("backbutton", function (e) {
    if (toRun)    
    {
        //Do Something
        toRun = false;
    }
});

根据需要设置布尔值。如果是第一次,检查执行代码。否则什么都不做。

希望能有所帮助。