2

I am trying to work with the back button with help from the PhoneGap Documentation and Override Android Backbutton behavior only works on the first page with PhoneGap.

My code:

<script>
document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
    alert("df");
        console.log("PhoneGap Ready!");
         document.addEventListener("backbutton", handleBackButton, false);
    }

    function handleBackButton() {
        console.log("Back Button Pressed!");
        alert("df");

    }
</script>

But I get this error:

05-21 16:00:03.248: E/Web Console(1615): TypeError: Result of expression 'PhoneGap.fireDocumentEvent' [undefined] is not a function. at undefined:1

Community
  • 1
  • 1
user1324068
  • 267
  • 3
  • 7
  • 17
  • Not sure if it's the same problem, but I solved my backbutton handling issues overriding onBackPressed in the activity class: @Override public void onBackPressed() { return; } – davids May 21 '12 at 11:12
  • i tried this also by http://stackoverflow.com/questions/10382634/how-to-prevent-android-from-closing-web-application-when-backbutton-is-pressed link.no sound – user1324068 May 21 '12 at 11:17
  • Ok, this answer is a bit silly, but just for make it sure, have you linked the correct cordova-x.js? I mean, you didn't attach the js for iOS, Blackberry, etc., did you? – davids May 21 '12 at 11:27
  • ok i got i change the js file thank you] – user1324068 May 21 '12 at 11:35
  • Did it work? In that case I'll post an answer – davids May 21 '12 at 11:39
  • ya work,Suppose i want to navigate the previous page in back button wat can i do – user1324068 May 21 '12 at 11:53

1 Answers1

3

Check that you are linking the correct phonegap-x.js for the platform, the javascript code is different for android, iOS, etc.

When the back button is pressed in Android, the event 'backbutton' is fired, so if you want to go back in the navigation history, what you should do is attaching the following handler to it:

document.addEventListener("backbutton", function(e){
    e.preventDefault();
    navigator.app.backHistory();
}, true);
davids
  • 6,259
  • 3
  • 29
  • 50