1

I´m building a project using JQuery Mobile and PhoneGap.

Is there a way to exit an app when the user hits the back button (standard way of exiting an app on android) but you still have alot of "history" in the webview.

Problem i´m having that the user can navigate through alot of pages and then i provided a button for them to go directly to the startpage. And if they hit the phones back button they will exit the app.

I have read about navigator.app.exitApp() but can´t find any docs on it on PhoneGap (or maybe it isn´t PhoneGap code?)

Juw
  • 2,059
  • 4
  • 18
  • 23
  • 1
    See my answer to a similar quersion here: http://stackoverflow.com/questions/8602722/phonegap-android-back-button-close-app-with-back-button-on-homepage/8662401#8662401 – Spadar Shut Jul 12 '12 at 16:06
  • Yeah, but will it work in PhoneGap 1.9.0? And where does the navigator.app.exitApp(); come from? Is there any docs on it? – Juw Jul 12 '12 at 16:42
  • 1
    I'ts provided by Phonegap but it's not documented: https://groups.google.com/forum/?fromgroups#!topic/phonegap/dnupz7DFZLY I just found it somewhere on the internets. – Spadar Shut Jul 12 '12 at 17:12

1 Answers1

3

im using this code to close my app.

add this Override at your Main Activity (Java Class)

@Override
public void onBackPressed() {
    super.loadUrl("javascript:onBackKeyDown()");
    return;
}

in your HTML:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
function onBackKeyDown() {
    // Exit the app!
    navigator.app.exitApp();
}
IRvanFauziE
  • 823
  • 11
  • 16