Back button event not working properly in android

  • 3
  • Question
  • Updated 4 years ago
  • Answered
I am trying to show notification, when user tap the back button in android device using phonegap.

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() {
}

with this am not acheiving the thing,

please suggest me to get in right way.

Thanks in advance
Photo of manoj chandra

manoj chandra

  • 6 Posts
  • 0 Reply Likes

Posted 7 years ago

  • 3
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Manoj,

Could I inquire what you expect to happen?

Your backbutton function is empty meaning,

1. You have overwritten backbutton behaviour hence would not work as expected
2. You did not tell backbutton what to do.

Do let us know exactly what you intend it to do so we can advise further.

Thanks.

Ismael
Photo of manoj chandra

manoj chandra

  • 6 Posts
  • 0 Reply Likes
Thanks for reply Ismael

when user taps back button, i need to show prompt box.

After entering password, it allows him to go out of my app.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Manoj,

Your code should look like this:

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() {
navigator.notification.prompt(
'Please enter your Password', // message
onPrompt, // callback to invoke
'Registration', // title
['Ok','Exit'], // buttonLabels
'' // defaultText
);
}

function onPrompt(results) {
if((results.buttonIndex == 1)&&(results.input1 == storedpswd)){ //Validate against stored password
navigator.app.exitApp();
}
}

I am not saying this is the exact code but it is pretty close.

Ismael
Photo of manoj chandra

manoj chandra

  • 6 Posts
  • 0 Reply Likes
Thanks for reply,

with ur code am able to override the back button.

But navigator.app.exitApp() not working, how to overcome this.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Manoj,

Please what platform are you targeting?

If it is iOS, you cannot exit app unless user presses home button, if it's Windows, you would have to check for an equivalent to the above method, however if it is Android, I will test this out to confirm that it isn't working.

Thanks for the update.

Ismael
Photo of manoj chandra

manoj chandra

  • 6 Posts
  • 0 Reply Likes
am targeting for android platform

Thanks
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Manoj,

Sorry for the late reply.

I written a sample project which can be found here in which when user presses the backbutton, he is prompted if he wants to exit and selecting exit actually exits the app.

This was tested to work fine on Nexus 4 (Android 4.3) works on PhoneGap Build 2.5, 2.9 and 3.0.

Send over a sample code so I can have a look and see what could be wrong.

Thanks.

Ismael
Photo of Kene Ijezie

Kene Ijezie

  • 6 Posts
  • 0 Reply Likes
Hi Ismael, I have tried using the exitApp() function for an android app I'm building but I can't seem to get it to work. I am also using PGB 3.0. (see my html page below). Can you help?

<!DOCTYPE html SYSTEM>

MY APP

Photo of Kene Ijezie

Kene Ijezie

  • 6 Posts
  • 0 Reply Likes
Sorry...see code below:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<script type="text/javascript" src="phonegap.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.mobile-1.3.1.min.js"></script>
<script>
function exitAppPopup() {
navigator.notification.confirm(
'Exit PETAN Mobile App?'
, function(button) {
if (button == 2) {
navigator.app.exitApp();
}
}
, 'Exit'
, 'No,Yes'
);
return false;
}
document.addEventListener("backbutton", function() {
if ( $('.ui-page-active').attr('id') == 'mainpage') {
exitAppPopup();
} else {
history.back();
}
}, false);
$(document).on('vclick', '#closeBtn a', function(){
exitAppPopup();

});
</script>
</head>
<body>

<div data-role="page" id="mainpage">

<div data-role="content" role="main" class="myBg">

<div id="branding">
<h1>MY APP</h1>
</div>

<ul data-role="listview" data-inset="true" data-theme="d">
<li><a href="members.html" rel="external"><i class="icon-group"></i> Members Directory</a></li>
<li><a href="execmembers.html"><i class="icon-suitcase"></i> Executives</a></li>
<li><a href="events.html" rel="external"><i class="icon-calendar"></i> Upcoming Events</a></li>
<li><a href="news.html" rel="external"><i class="icon-bullhorn"></i> Oil & Gas News</a></li>
<li><a href="media.html" rel="external"><i class="icon-picture"></i> Photo Gallery</a></li>
<li id="closeBtn"><a href="#close" rel="external"><i class="icon-off"></i> Exit</a></li>
</ul>
</div>

</div>

</body>
</html>
Photo of Ashish Kadam

Ashish Kadam

  • 1 Post
  • 0 Reply Likes
backButton is working on cordova 3.6.0 but not on 5.1.1 ,I am using android platform
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Ashish,
this thread is over 3 years old. It is also marked "ANSWERED". If you cannot find the answer on this thread, start a new thread. AND explain with more details.
Jesse