sam
sam

Reputation: 39

Phonegap events not firing

I am creating a phonegap application for android and want to use some of the phonegap events like "resume", "pause", "backbutton" etc. but none of these events are getting fired except the "deviceready" event. Following is my javascript code please check if I am making any mistake:

function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        // device APIs are available
        //
        function onDeviceReady() {
            alert("Device Ready");
            document.addEventListener("resume", onResume, false);
            document.addEventListener("backbutton", onBackKeyDown, false);
        }

        // Handle the resume event
        //
        function onResume() {
            setTimeout(function() {
            alert("onResume");
            }, 0);
        }

        function onBackKeyDown() {
            // Handle the back button
            setTimeout(function() {
            alert("onBackKeyDown");
            }, 0);
        }

the alert inside 'onDeviceReady()' function is working.

please help, thanks in advance.

Upvotes: 0

Views: 439

Answers (1)

bomblike
bomblike

Reputation: 168

Things you should try / check:

1) On HTML are you calling onLoad()?

2) On HTML are you referencing the js?

<!DOCTYPE html>
<html>
    <head>
    <title>Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="example.js"></script> //(2)
    </head>
    <body onload="onLoad()"> //(1)
    </body>
</html>

3) On JS put the event listener onDeviceReady outside onLoad()

document.addEventListener("deviceready", onDeviceReady, false);
function onLoad() {}
function onDeviceReady() {
  alert("Device Ready");
  document.addEventListener("resume", onResume, false);
  document.addEventListener("backbutton", onBackKeyDown, false);
}

Upvotes: 0

Related Questions