0
votes

In my Android phonegap/Cordova 3.5 project, I have 3 html pages (a button in index.html calls page2.html where there is another button calling page3.html).

In page2.html I override backbutton doing this in deviceReady:

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

then

function onBackKeyDown() {
            console.log("onBackKeyDown");
            navigator.app.exitApp();
        }

but in page3.html I want to let Android controlling backbutton so I do not override it in page3.html. Instead in page3.html backbutton is disabled.

Questions:

  1. Is this the expected behaviour (overriding one means overriding for ever)?
  2. How to get rid with this? (leave the control of backbutton to Android anytime I want to)
3

3 Answers

1
votes

You can check the page in the OnBackKeyDown event:

function onBackKeyDown() {
  var pagename = // get the page name here
  if ( pagename == page3.html ) {
    history.back();
  }
  else {
    navigator.app.exitApp();
  }
}
0
votes

You can back button behaviour according to each page. Example is shown below.

function onBackKeyDown(e) {
        if ($.mobile.activePage[0].id == "home"
            || $.mobile.activePage[0].id == "login") {
            e.preventDefault();
            navigator.app.exitApp();
        }else if($.mobile.activePage[0].id == "inspection"){
            e.preventDefault();
            back();
            //return false;
        }else if($.mobile.activePage[0].id == "noDetails"){
            e.preventDefault();
            $.mobile.changePage("home.html");
            return false;
        }else{
            navigator.app.backHistory();
        }

    }

I am using jQuery Mobile- gives more control over page transitions.

0
votes

the real solution to this question is probably to remove the event with this API:

document.removeEventListener("backbutton", onBackKeyDown, false);

I found it here