Danmoreng
Danmoreng

Reputation: 2367

Cordova Android Disable Back Button [doesn't work]

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?

Upvotes: 0

Views: 3796

Answers (3)

Lahiru Supunchandra
Lahiru Supunchandra

Reputation: 376

<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.

Upvotes: 0

Danmoreng
Danmoreng

Reputation: 2367

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>  

Upvotes: 1

Michael Laffargue
Michael Laffargue

Reputation: 10314

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

Upvotes: 1

Related Questions