Rvervuurt
Rvervuurt

Reputation: 8963

Close Android app Jquery - Phonegap

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:

What am I doing wrong?

Upvotes: 1

Views: 9075

Answers (2)

judgement
judgement

Reputation: 437

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

Upvotes: 8

Simon MacDonald
Simon MacDonald

Reputation: 23273

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?

Upvotes: 0

Related Questions