0
<!DOCTYPE html>
<html>
  <head>
    <title>PhoneGap Back Button Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Call onDeviceReady when PhoneGap is loaded.
    //
    // At this point, the document has loaded but phonegap-1.2.0.js has not.
    // When PhoneGap is loaded and talking with the native device,
    // it will call the event `deviceready`.
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is loaded and it is now safe to call PhoneGap methods
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    // Handle the back button
    //
    function onBackKeyDown() {alert("back button pressed");
    }

    </script>
  </head>
  <body onload="onLoad()">
  </body>
</html>

I want to ask if user want to exit from my app, when it click on back button.

I read this example from http://docs.phonegap.com/en/1.2.0/phonegap_events_events.md.html#backbutton but it is not working...

How can I do to do it?

I SET SOME ALERT IN ONDEVICEREADY FUNCTION, AND I HAVE SEEN THAT IS NEVER FIRED THIS METHOD....SO THE EVENTLISTENERE DEVICEREADY IS NEVER FIRED...WHY????? Thanks a lot.

michele
  • 26,348
  • 30
  • 111
  • 168

2 Answers2

0

It looks like you should definitely see the alert based on your code and your link. However the tutorial is for version 1.2 and you're using version 2.2. You may want to check that for tutorials using the same version. If one doesn't exist then the functionality may have been removed or you'll need to discuss with the Cordova developers about this problem.

Nick Roth
  • 3,057
  • 2
  • 15
  • 15
  • I SET SOME ALERT IN ONDEVICEREADY FUNCTION, AND I HAVE SEEN THAT IS NEVER FIRED THIS METHOD....SO THE EVENTLISTENERE DEVICEREADY IS NEVER FIRED...WHY????? – michele Nov 29 '12 at 15:18
  • If deviceready is not firing then you likely have a problem with your configuration or deployment. – Nick Roth Nov 29 '12 at 16:06
0

If onDeviceReady() is not fired means that PhoneGap is not yet loaded. Please make sure that your cordova-2.2.0.js file is in the correct location. As per your sample ".html" and "cordova-2.2.0.js" file should be in the same folder

Soney Antony
  • 115
  • 9