Disable Device Backbutton Globally

Posted by spserna2 on 15-Jan-2015 05:45

Hi all, 

I'm trying to define custom behavior for the device back button (Android).
However, there are no action available for the backbutton event on the page level.

After the second thought,  I would like to disable the default backbutton behaviour globally  (or delete entire back history) if possible. I will provide custom navigation flow for the user.

Anyone had experience dealing with the backbutton behaviour?


It would be great if you could shed the light, thanks...
 

Posted by egarcia on 16-Jan-2015 12:50

Hello,

Yes, the event seems to be at the document level so you would only need to do this code once. The Login page (the start page) seems to be just fine.

From the Mobile App Builder, you can do the code in a RUN JavaScript action for Load of the start page.

The backKeyDown() function could be in a separate JavaScript file.

Alternatively, you could have:the following code in a separate JavaScript and the call to ready() would execute when the JavaScript file is loaded.

$(document).ready(function() {

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

       console.log("PhoneGap is ready");

});

function backKeyDown() {

       // Action for BackKeyDown

}

I hope this helps.

All Replies

Posted by egarcia on 15-Jan-2015 06:26

Hello,

I have not actually tried this. However. I found a link that suggests that you can listen to the backbutton event to override the button behavior. (Ideally, your implementation is still consistent with the OS behavior.)

stackoverflow.com/.../how-do-i-disable-the-phones-back-button-in-android-using-jquerymobile-phonegap

I hope this helps.

Posted by spserna2 on 16-Jan-2015 12:37

function onDeviceReady() {
        document.addEventListener("backbutton", backKeyDown, true);
        console.log("PhoneGap is ready");
    }

    function backKeyDown() {
        navigator.app.exitApp(); // To exit the app!
    }


Thanks egarcia,
Do you mean that I will need to add these code to every page of PageShow event ?

Is it possible to only subscribe to the event once?
For example, on the Login page.

Or at the global scope?

Thank you.

Posted by egarcia on 16-Jan-2015 12:50

Hello,

Yes, the event seems to be at the document level so you would only need to do this code once. The Login page (the start page) seems to be just fine.

From the Mobile App Builder, you can do the code in a RUN JavaScript action for Load of the start page.

The backKeyDown() function could be in a separate JavaScript file.

Alternatively, you could have:the following code in a separate JavaScript and the call to ready() would execute when the JavaScript file is loaded.

$(document).ready(function() {

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

       console.log("PhoneGap is ready");

});

function backKeyDown() {

       // Action for BackKeyDown

}

I hope this helps.

This thread is closed