Software Archive
Read-only legacy content
17061 Discussions

device backbutton event does not fire

X_2
Beginner
1,102 Views

Testing Android app with App Preview on Galaxy S5 Android version 5.0. Also debugging via USB debugging through Chrome. Intel XDK version 2673. Tried using Cordova version 4.1.2 and 5.1.1. Tried with and without the Cordova device plugin.

Couldn't capture the back button event on my main project, so I made a side simple HTML5 + Cordova project and copied in the back button event template from the Cordova docs, and the onLoad and DeviceReady functions both fire, but the backbutton event is never captured - just like on my main project. Any help would be appreciated:

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

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

    <script type="text/javascript" charset="utf-8">

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

    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    function onBackKeyDown() {
        alert("HI");
    }

    </script>

  </head>
  <body onload="onLoad()">
      back doesn't work
  </body>
</html>

 

0 Kudos
4 Replies
Elroy_A_Intel
Employee
1,102 Views

In order to capture the pressing of the back button on your Android device, you are required  to add the following code:

document.addEventListener("backbutton", yourCallbackFunction, false);

function yourCallbackFunction() {
    //Handle the back button
}

The above event handler is supported on Android. For more information in regards to this and other Cordova lifecycle events, visit https://cordova.apache.org/docs/en/4.0.0/cordova/events/events.html for Cordova 4 and https://cordova.apache.org/docs/en/latest/cordova/events/events.html for Cordova 5.

0 Kudos
X_2
Beginner
1,102 Views

That is literally the code I posted above, saying it is not working. And you link me to the page where I said I got the code from. Can you try to provide some reason as to why my code may not be working? 

0 Kudos
PaulF_IntelCorp
Employee
1,102 Views

I think there's a bug in App Preview that prevents you from seeing that backbutton event. Try a building a simple app and testing it in a built app.

0 Kudos
X_2
Beginner
1,102 Views

Paul you are correct. We released a beta of our app to the store for testing, downloaded it through there and the back button trigger worked properly.

0 Kudos
Reply