Medo Medo
Medo Medo

Reputation: 1018

backbutton confirm exit app android + phonegap + jquery

How can I handle the Physical Back Button in Android by using jQuery Mobile within Phonegap online system? I want to show confirm to exit the app (YES - NO). I tried many ways but nothing is working.

Upvotes: 1

Views: 23401

Answers (7)

Code9
Code9

Reputation: 1

This worked for me.

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

Use this

<script type="text/javascript">
    document.addEventListener("deviceready", onDeviceReady, false);

        function onDeviceReady(){
            document.addEventListener("backbutton", function(e) {
                e.preventDefault();

                        if(confirm("Exit App?")) {
                            navigator.app.exitApp();
                        }
            }, false);
        }
    </script>

or

<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady(){
        document.addEventListener("backbutton", function(e) {
            e.preventDefault();

            press = press + 1;
            if(press == 1) {
                alert('Press again to exit');
            }

            if(press == 2) {
                navigator.app.exitApp();
            }
        }, false);
    }
</script>

Upvotes: 0

viki
viki

Reputation: 11

Try this:

// When Device ready
document.addEventListener('deviceready', function() {
    document.addEventListener("clickBackbutton", ExitDialogPrompt, false);
}, false); 

function ExitDialogPrompt() {
    navigator.notification.confirm(
        ("Do you want to Exit?"), // message
        prompt, // callback
        'Your title', // title
        'YES,NO' // button Name
    );
}

function alertexit(button){
    if (button=="0" || button==1) {
       navigator.app.exitApp();
    }
}

Upvotes: 0

SWAPNIL KUWAR
SWAPNIL KUWAR

Reputation: 422

If user clicks on yes then exit the app. First install the dialogs plugin by running cordova plugin add cordova-plugin-dialogs

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

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}

function onBackKeyDown(e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}

function onConfirm(button) {
    if(button==1){//If User selected Yes, then exit app
        navigator.app.exitApp();
    } else {
        return;
    }
}

Upvotes: 0

Lloyd Zhou
Lloyd Zhou

Reputation: 51

when first click the set exitApp = true, so second time to click the backbutton will exit the app. but set one Interval to change the status of exitApp to false. so when click backbutton twice in 1 second, will exit the app.

document.addEventListener('deviceready', function() {
    var exitApp = false, intval = setInterval(function (){exitApp = false;}, 1000);
    document.addEventListener("backbutton", function (e){
        e.preventDefault();
        if (exitApp) {
            clearInterval(intval) 
            (navigator.app && navigator.app.exitApp()) || (device && device.exitApp())
        }
        else {
            exitApp = true
            history.back(1);
        } 
    }, false);
}, false);

Upvotes: 5

zstew
zstew

Reputation: 1375

Not sure if phonegap changed or what but Suhas and geet's solution only almost worked for me. (but definitely gave me what I needed to make it work, thanks!) The back button basically got broken.

here are the tweaks that made it work for me:

once your app is loaded do this:

        document.addEventListener("backbutton", onBackKeyDown, false);//hijack the backbutton

        function onBackKeyDown(e){
            var page = $.mobile.activePage.attr('id');
            xStat.rec("back button clicked from page:" + page);
            if (page == 'menuPage'){//are you on the 'root page' from which phonegap will exit?
                e.preventDefault();
                $.mobile.changePage('#aboutToExitAppPage');
            } else {
                window.history.back();//restore normal back button functionality
            }
        }


//somewhere else in your code for the "aboutToExit app" page
        $('#aboutToExitAppPage').on('pageinit', function(){
            $(this).find('#exitApp').on('click', function(){
                navigator.app.exitApp();//quit the app.
            });
        });

and the HTML

<div data-role="page" id="aboutToExitAppPage">
    <div data-role="header" id="" data-position="inline" data-backbtn="true" >
        <h1 class=>About to exit app</h1>
    </div>
    <div data-role="content" style="width:100%; height:100%; padding:0;">
        <ul id="" data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" data-role="fieldContain">
            <input id="exitApp" class="" type="button" value="Exit" data-theme="">
            <a href="#menuPage" data-role='button'>Main Menu</a>
        </ul>
    </div>
</div>

Upvotes: 1

Suhas Gosavi
Suhas Gosavi

Reputation: 2180

Try this:

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

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}

function onBackKeyDown(e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}

function onConfirm(button) {
    if(button==2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

Upvotes: 8

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

//Deviceready function

document.addEventListener('deviceready', function() {

    document.addEventListener("backbutton", ShowExitDialog, false);

}, false);

// Dialog box when back button is pressed

 function ShowExitDialog() {
        navigator.notification.confirm(
                ("Do you want to Exit?"), // message
                alertexit, // callback
                'My APp', // title
                'YES,NO' // buttonName
        );

    }

// Call exit function

 function alertexit(button){

        if(button=="1" || button==1)
        {

            device.exitApp();
        }

}

Upvotes: 2

Related Questions