0

I found this: How do I disable Android Back button on one page and change to exit button on every other page

and this: Android - Disable Device Back button

Which both points to the solution adding an EventListener for the Backbutton and preventing its default action.

However, this doesn't seem to work for my application which basically just consists of several HTML pages + some Javascript and CSS. The Hardware Android Back button will work although I added this code in my Javascript as suggested in both solutions:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    document.addEventListener("backbutton", function (e) {
        e.preventDefault();
    }, false );}

What am I doing wrong? What else can I do?

Community
  • 1
  • 1
Danmoreng
  • 2,367
  • 1
  • 19
  • 32

3 Answers3

1

It also says to ensure you did add the mobile version of cordova script. Did you?

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
1

Never mind I am dumb. For the events to work the cordova.js has to be included in the HTML pages or it won't work:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>  
Danmoreng
  • 2,367
  • 1
  • 19
  • 32
0
<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);
      }

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

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

</script>

<body onload="onLoad()">

You can read the Cordova documentation for more information.

Adriaan
  • 17,741
  • 7
  • 42
  • 75