PhoneGap后退按钮退出应用程序

PhoneGap Back Button exit the Application

本文关键字:退出 应用程序 按钮 PhoneGap      更新时间:2023-09-26

目前我正在使用PhoneGap(Cordova 2.2)、JQuery和Javascript开发移动应用程序。登录页是登录页。因此,一旦我使用登录凭据进入仪表板页面,当我单击BACK BUTTON时,它将返回到登录页面,而不会停留在仪表板页面上。我能做什么???欢迎提出建议。

我试过了,

脚本

<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.2.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
    alert("Back Button Clicked"); //called the alert..checking
}
</script>

HTML

<body onload="onLoad()">
</body>

我的onDeviceReady和onBackKeyDown函数不工作/已激发。我是不是错过了什么???

尝试使用loggin变量检查是否登录,并对回调函数执行某些操作。

function onBackKeyDown(evt) {
     evt.preventDefault();
     evt.stopPropagation();
     if(loggedin=='yes'){
         //dont do anything or show popup
     }else{
         history.back();
     }
}

您需要首先绑定eventLinstener,然后在页面onLoad OR$(document).ready()方法中调用app.initialize()

 var app = {
 // Application Constructor
   initialize: function() {
       this.bindEvents();
   },
   // Bind any events that are required on startup. Common events are:
   // 'load', 'deviceready', 'offline', and 'online'.
   bindEvents: function() {
      document.addEventListener('deviceready', this.onDeviceReady, false);
   },
   onDeviceReady: function() {
       document.addEventListener("backbutton", onBackKeyDown, false);
   }
};

试试这个

    //after successful  login
        localStorage.setItem('login', 1);
// and your back function callback
        function onBackKeyDown(e) {
        e.preventDefault();
        e.stopPropagation();
        var isLogin = localStorage.getItem('login);
        if (isLogin){
        // stay here
        } else {
        history.back();
        }
           }