53

I am developing a Android app using Jquery Mobile/Phonegap. I have the following code to control the phone's back button:

document.addEventListener("backbutton", backKeyDown, true); 


function backKeyDown() { 
    // Call my back key code here.
    $.mobile.changePage("#homepage", "slideup");
}

This all works fine, but I would like the app to close when pressing the back button on the homepage only, is this possible?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Dancer
  • 17,035
  • 38
  • 129
  • 206

4 Answers4

121

Update: this has stopped working with a latest Phonegap update (supposedly). Feel free to offer a working solution if you know it.


Here's how I do it:

document.addEventListener("backbutton", function(e){
    if($.mobile.activePage.is('#homepage')){
        /* 
         Event preventDefault/stopPropagation not required as adding backbutton
          listener itself override the default behaviour. Refer below PhoneGap link.
        */
        //e.preventDefault();
        navigator.app.exitApp();
    }
    else {
        navigator.app.backHistory()
    }
}, false);

For further information, here you can find the related documentation with a full example: http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#backbutton

Spadar Shut
  • 15,111
  • 5
  • 47
  • 54
12
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown() 
{
 navigator.app.exitApp();
}

Thank you spader.

Thomas V J
  • 658
  • 8
  • 7
  • 1
    is it necesssary to wrap the backbutton listener within a deviceready listener as shown here? I've seen the 'backbutton' solution described several times in different places (including elsewhere on stackoverflow) and none show it specifically wrapped like you have here. Would be good if someone can clarify – Andy Lorenz May 13 '14 at 22:55
9

You would need to keep track of when the homepage is being displayed. When you know you are on the homepage call:

navigator.app.exitApp();
Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Where is the phonegap doc for navigator.app.exitApp() [and other navigator.app extensions]? I couldn't find it at http://docs.phonegap.com/en/2.2.0/index.html – Samik R Dec 30 '12 at 23:08
  • It's not documented since it is not cross platform. If you want to learn what is available for Android then open up the cordova.js in your text editor and search for "cordova/plugin/android/app". There is actually a few good comments in the code that can help you figure out what is going on. – Simon MacDonald Dec 31 '12 at 13:47
  • Thanks Simon - will check it out. – Samik R Jan 01 '13 at 16:53
1

If you don't want to use jQuery Mobile, change $.mobile.activePage.is('#homepage') to document.getElementById('#homepage') on @Spadar Shut answer, as on following code:

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

    function onDeviceReady(){
        document.addEventListener("backbutton", function(e){
           if(document.getElementById('#homepage')){
               e.preventDefault();
               navigator.app.exitApp();
           }
           else {
               navigator.app.backHistory()
           }
        }, false);
    }

Through this way, don't need to download Jquery Mobile gibberish only for this purpose. Also, activePage is deprecated as of JQuery mobile 1.4.0 and will be removed from 1.5.0. (Use the getActivePage() method from the pagecontainer widget instead)

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
meetnick
  • 1,196
  • 2
  • 12
  • 28
  • How to set homepage to my index.html? i.e. document.getElementById('#homepage') doesn't work. – Zac Jun 26 '17 at 04:13