Skip to content Skip to sidebar Skip to footer

How To Prevent Android From Closing Web-application When Backbutton Is Pressed?

I am developing a HTML5 web-application and compiling it with Cordova (phonegap) 1.7. I want to override the Android backbutton so that I can call window.history.back() instead of

Solution 1:

I solved my own question by adding the code below to the DefaultActivity.java file to prevent the default android behavior, and keeping the JavaScript code as stated in the question:

@OverridepublicvoidonBackPressed() {
   return;
}

I hope this helps someone in the future with the same problem!

Solution 2:

I took this approach. I hooked the backbutton event as you have shown. I look to see if this is the first page or not and then ask the user if they want to exit the program or not. This depends on what you want your program to do depending on its state. I did not add the override as you have shown; I didnot seem to need it.

if ($.mobile.activePage.attr('id') === 'firstpage') {
    // Prompt to confirm the exit
} else {
  window.history.back();
}

If they want to exit you can call:

navigator.app.exitApp();

to close your program.

I imagine you still want to allow the user to exit your app. I don't tend to use apps that do not allow an exit of some kind.

Hope this helps you out.

Solution 3:

Never had to do that but, have you tried to return true ?

Like in the Java SDK, if you return True, the system will assume you have correctly catched the event and will no longer pass it to other event listeners.

Post a Comment for "How To Prevent Android From Closing Web-application When Backbutton Is Pressed?"