简体   繁体   English

Android:无法捕获JavaScript中的后退按钮

[英]Android: Unable to capture the backbutton click in javascript

I am using IntelXDK to develop an android app in HTML5, CSS3 and JavaScript, the app is a one page app which works fine while switching views with HTML buttons but when pressing the back button on a mobile device the app exits, however I want to capture this event and display the home screen. 我正在使用IntelXDK在HTML5,CSS3和JavaScript中开发一个Android应用程序,该应用程序是一页应用程序,在使用HTML按钮切换视图时可以正常工作,但是在移动设备上按返回按钮时,该应用程序退出了,但是我想捕获此事件并显示主屏幕。

The event works in a simulator but not on a physical device 该事件在模拟器中有效,但在物理设备上无效

This is what I have done to capture the backbutton click event but the app just closes: 这是我捕获后退按钮单击事件的方法,但是该应用程序仅关闭:

$(document).ready(function () {

    var animTime = 300,
        clickPolice = false;

    $(document).on('click', '.acc-btn', function () {
        if (!clickPolice) {
            clickPolice = true;

            var currIndex = $(this).index('.acc-btn');

            $('.acc-content').stop().animate({ height: 0 }, animTime);
            $('.acc-content').eq(currIndex).stop().animate({ height: 108 }, animTime);

            setTimeout(function () { clickPolice = false; }, animTime);
        }
    });

    //Back button event
    document.addEventListener('backbutton', function () {
        if ($('#front').hasClass('hidden')) {
            ShowPage('front');
            return false;
        }
        else {
            navigator.app.exitApp();
        }
    }, false);
});

//Show page function etc.....

I know the ShowPage function works fine as it is used elsewhere 我知道ShowPage函数可以正常工作,因为它在其他地方使用

Any help is appreciated 任何帮助表示赞赏

Check out the following link for Android - https://software.intel.com/en-us/node/493108 请查看以下Android链接-https: //software.intel.com/zh-cn/node/493108

You have to capture the back button and add a virtual page to prevent the app from exiting 您必须捕获“后退”按钮并添加虚拟页面,以防止应用退出

document.addEventListener("intel.xdk.device.hardware.back", function() {
//continue to grab the back button
   intel.xdk.device.addVirtualPage(); 

}, false); 

Try to remove the boolean parameter from your addEventListener so 尝试从您的addEventListener中删除布尔参数,因此

//Back button event
document.addEventListener('backbutton', function () {
    if ($('#front').hasClass('hidden')) {
        ShowPage('front');
        return false;
    }
    else {
        navigator.app.exitApp();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM