PhoneGap处理后退按钮不关闭应用程序

时间:2013-04-21 18:21:23

标签: java android cordova back-button

我有index.html和page.html

index.html有一个到page.html的锚点

我会按下按钮并向用户询问他是否真的要回到index.html

这是我的代码:

的index.html

<!DOCTYPE HTML>
<html>
  <head>
    <title>First App</title>
    <script src="cordova-2.6.0.js"></script>
    <script>
     function onLoad(){
          document.addEventListener("deviceready",onDeviceReady, true);
     }

     function onDeviceReady(){
        navigator.notification.alert("PhoneGap is working!!");
     }
  </script>
  </head>
  <body onload="onLoad();">
       <h1>Welcome to PhoneGap</h1>
       <h2>Edit assets/www/index.html</h2>
        <a href="page.html">Go to page</a>
  </body>
</html>

page.html中

<!DOCTYPE HTML>
<html>
  <head>
    <title>First App</title>
  <script src="cordova-2.6.0.js"></script>
  <script>
     function onLoad(){
          document.addEventListener("deviceready",onDeviceReady, true);
        document.addEventListener("backbutton", onBackKeyDown, false);
     }

     function onDeviceReady(){
navigator.notification.alert("PhoneGap is working!!");
     }

function onBackKeyDown(e) {        
        navigator.notification.alert("PhoneGap back is working!!");
}
  </script>
  </head>
  <body onload="onLoad();">
       <h1>Welcome to PhoneGap Page</h1>
       <h2>Edit assets/www/page.html</h2>
  </body>
</html>

问题是没有处理后退按钮,但正确加载了cordova,因为我显示了警告框。

我做错了什么?

我有Android谷歌Nexus和Android 4.2.2

非常感谢。

1 个答案:

答案 0 :(得分:3)

将你的后退按钮监听器放在onDeviceReady函数中。