Jeongbebs
Jeongbebs

Reputation: 4120

Disable back button Android but not on Home Page using PhoneGap

I have successfully disable the Android Hardware Back Button in my phonegap project. However, if the user is in the home page, I want the back button to function again and allow the user to go back the "list of apps" screen. Is there a way how to do this? Thanks

Upvotes: 1

Views: 1110

Answers (1)

xumet
xumet

Reputation: 216

If you know you are in the home page you can control the behaviour...

Something like this:

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

function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
}

function onBackKeyDown(ev){
    // If the class 'home' is present in the first div inside #container div
    var home = $("#container div:first-child").hasClass("home");

    if(home){
        console.log("exit App<----------");
        navigator.app.exitApp();
    }
    ev.preventDefault();
}

I control it using a class in a div, but it could be in any other way...

Upvotes: 2

Related Questions