简体   繁体   English

Phonegap后退按钮事件,是否检测到历史记录的第一页?

[英]Phonegap backbutton event, detect first page in history?

Adding a backbutton event listener to my Phonegap 2.0 mobile app prevents the user from exiting using the back key. 添加backbutton事件监听到我的PhoneGap 2.0移动应用防止使用返回键退出用户。

Before adding the event listener this was working: if the user visited N pages and clicked back N+1 times, the app would close (or go in the background for android 4.0 or higher). 在添加事件侦听器之前,这是可行的:如果用户访问了N个页面并单击了N + 1次,则该应用将关闭(或在Android 4.0或更高版本中进入后台)。

Please see my code bellow. 请参阅下面的代码。

document.addEventListener("backbutton", function(){
    if (window.history.length == 0) { // this does not work
        function quitApp(){
            navigator.app.exitApp();
        }
        navigator.notification.confirm(
            "Are you sure you want to quit?",
            quitApp,
            'App Title',
            'Cancel,Ok');
        return;
    }
    if (typeof(window.activePage.onBack) === 'function') {
        window.activePage.onBack();
    } else {
        window.history.back();          
    }
}, false);

Any idea how i can achieve this: allow the user to exit using the back button while keeping my event listener? 任何想法我怎么能做到这一点:允许用户使用后退按钮退出,同时保留我的事件监听器?

Thanks! 谢谢!

Try to bind the back button on your title page, maybe something like this: 尝试将标题页上的“后退”按钮绑定,也许是这样的:

$('#home-page-title').bind( 'pageinit',function(event){

      document.addEventListener("backbutton", function(){
           navigator.app.exitApp();
  }, false);

});

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

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