1

I'm trying to close my Phonegap Android app with the following piece of code:

document.addEventListener("backbutton", function () { 
    if ($('.screenshot').is(":visible")) {
        if (confirm('Afsluiten?')){
            setTimeout( function() { navigator.app.exitApp(); });
        }
        else {
            '';
        }
    }
    else {
        $(".items , .screenshot").show();
        $(".content , .openbrowser , .html5vid , .introtekst_gal" ).hide();
        $(".terug").hide();
    }
}, true);

It works once: Pressing the back-button and then "Ok" closes the app, as expected.

But when I do it like this, the app doesn't close anymore:

  • Press back-button (pop up shows)
  • Press "cancel" (pop up goes away)
  • Press back-button (pop up shows)
  • Press "ok" (pop up goes away and app SHOULD close, but doesn't)

What am I doing wrong?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Rvervuurt
  • 8,589
  • 8
  • 39
  • 62

2 Answers2

8

you can try this code:

     document.addEventListener("backbutton", function () { 
         navigator.notification.confirm(
             'Do you want to quit', 
             onConfirmQuit, 
             'QUIT TITLE', 
             'OK,Cancel'  
         );
     }, true); 


    function onConfirmQuit(button){
        if(button == "1"){
            navigator.app.exitApp(); 
        }
    }
Joffrey Maheo
  • 2,919
  • 2
  • 20
  • 23
judgement
  • 437
  • 3
  • 12
  • This worked! In some weird way my code and Simon's code only worked once. This one works multiple times, even though in the end it's the same thing that's happening. Extra kudos for the parts where I could adapt the text of the box :D – Rvervuurt Apr 13 '12 at 21:11
0

I had to strip down some of the code to test so I ended up using:

        document.addEventListener("backbutton", function () { 
            if (confirm('Afsluiten?')){
                setTimeout( function() { navigator.app.exitApp(); });
            }
            else {
                '';
            }
        }, true);  

and that seems to be working just fine. You should do some console.logs to see if the confirm dialog returns you to the correct spot. Also, what do you see in "adb logcat" when you run the app?

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74