Skip to content Skip to sidebar Skip to footer

How Do I Disable Android Back Button On One Page And Change To Exit Button On Every Other Page

I am developing an Android application using Phonegap that interacts with my Drupal site. I have re-assigned the Android 'Back' button to prompt the user to log off from the Drupal

Solution 1:

deviceready is important. If not used, sometimes you can get blocking Back button, sometimes not. Often in debug it works, in production no.

document.addEventListener("deviceready", onDeviceReady, false);
    functiononDeviceReady() {
        document.addEventListener("backbutton", function (e) {
            e.preventDefault();
        }, false );
}

EDIT 2013-11-03 Common mistake is that development is performed on desktop and cordova script is excluded. One then forgets to include the cordova script for the mobile version.

Solution 2:

Try this:

document.addEventListener("backbutton", function(e){
    if($.mobile.activePage.is('#login_page')){
        e.preventDefault();
    }
    else {
        if (confirm("Are you sure you want to logout?")) {
            /* Here is where my AJAX code for logging off goes */
        }
        else {
            returnfalse;
        }
    }
}, false);

Solution 3:

document.addEventListener("deviceready", onDeviceReady, false);
functiononDeviceReady() {
    document.addEventListener("backbutton", function (e) {
        e.preventDefault();
    }, false );
}

Solution 4:

You have to override back button action in device ready... here is a full working example "index.html"

<!DOCTYPE html><html><head><title>Back Button</title><linkrel="stylesheet"type="tex`enter code here`t/css"href="style.css"><script>functiongetTitle() {
            document.getElementById("ct").innerHTML = "DEMO: " + document.title;
        }
    </script><scripttype="text/javascript"charset="utf-8"src="cordova.js"></script><scripttype="text/javascript"charset="utf-8">// Wait for device API libraries to load//functiononLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
        getTitle();
    }
    // device APIs are available//functiononDeviceReady() {
        // Register the event listenerdocument.addEventListener("backbutton", onBackKeyDown, false);
    }
    // Handle the back button//functiononBackKeyDown() {
        alert('Backbutton key pressed');
        console.log('Backbutton key pressed');
    }
    </script></head><bodyonload="onLoad()"><ulid="nav"><li><ahref="index.html">&larr; Back</a></li><li><ahref="#"id="ct"onclick="location.reload();"></a></li></ul></body></html>

I used this code and back button is overridden finally....

Ref--> https://github.com/amirudin/PhoneGapPluginDemo/blob/master/www/evt-backbutton.html

Post a Comment for "How Do I Disable Android Back Button On One Page And Change To Exit Button On Every Other Page"