Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

cordova - Stop Exit on Back Button Android in PhoneGap - Build

I'm building my application through PhoneGap Build online. I want to change the default behavior of Back Button

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

function onBackKeyDown(){
    alert('back');
    return false;
}

Its not working I've searched for solution. But, they all showing to change the java codes in PhoneGap library, which is not in my case. I'm submitting my application in a .zip format with config.xml inside.

Is it possible with config.xml?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all I did wrong as pointed by @Mejo, Thanks. Here is the solution to the problem.

Step 1: Include Script to HTML don't need it physically within application zip, as included automatically by PhoneGap Build

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

Step 2: Add this to script to get device ready call:

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

Step 3: Add event listener to back button and add your code to that call:

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

Still now it will not work if you don't set preference of minSDK to application by config.xml

Step 4: Add this to preference region of config.xml

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...