Stop Exit on Back Button Android in PhoneGap - Build

I create my application through PhoneGap Build online. I want to change the default behavior of the back button

$(document).ready(function(e) { document.addEventListener("backbutton", onBackKeyDown, false); }); function onBackKeyDown(){ alert('back'); return false; } 

Doesn't work I was looking for a solution. But they all demonstrate a change in java codes in the PhoneGap library, which is not in my case. I am sending my application in .zip format with config.xml inside.

Is this possible with config.xml?

+6
source share
2 answers

First of all, I did not do what @Mejo pointed out, thanks. Here is the solution to the problem.

Step 1: You do not need to include Script in HTML physically in a zip application, as PhoneGap Build is automatically added

<script src="cordova.js"></script> or <script src="phonegap.js"></script> any of them will work fine.

Step 2: Add this to Script to call the device :

 document.addEventListener("deviceready", onDeviceReady, false); 

Step 3: Add an event listener to the button and add code to this call:

 function onDeviceReady(){ document.addEventListener("backbutton", onBackKeyDown, false); } function onBackKeyDown(){ alert('back'); return false; } 

Now this will not work if you do not set the minSDK preference for the application by config.xml

Step 4: Add This To The Config.xml Preferences Area

<preference name="android-minSdkVersion" value="5" />

For reference: http://community.phonegap.com/nitobi/topics/how_to_handle_back_button_in_android

+6
source

As stated in the Cordoba API documentation, that

Usually you need to attach an event listener using document.addEventListener after receiving the PhoneGap 'deviceready' event.

So change your code as follows

 document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is loaded and it is now safe to make calls PhoneGap methods function onDeviceReady() { // Register the event listener document.addEventListener("backbutton", onBackKeyDown, false); } // Handle the back button function onBackKeyDown() { //Your backbutton code } 
+3
source

All Articles