backbutton in phonegap not getting registered and not working properly

I am using phonegapand i am registering backbutton onDeviceReady Function but getting function called when clicked on device’s back button. and i have added

<script src="lib/cordova-2.6.0.js"></script>

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

function onBackClickEvent() {
    alert("back onBackClickEvent");     
}

This onBackClickEvent() function is not getting called, I’ve never seen that alert poped up. I am also getting an error of Uncaught ReferenceError: cordova is not defined

  • Android: Screen touch event through ADB
  • Rectangle shape drawable, specify top and bottom stroke colors?
  • Custom actions in Contacts app (similar to G+)
  • google cloud messaging security
  • OBJ vs 3DS - Whats the best format for Android 3D Development
  • Local Notifications in Android?
  • what could be the error Please suggest me. Thanks in advance.

    Related posts:

    Android : How to set acceptable numbers and characters in EditText?
    Force Wi-Fi network for 3rd party apps
    How can I pull databases off my android onto my desktop?
    Get margin of a View
    How to allow the user to select a text range in a TextView (Similar to EditText)
    FragmentTransaction before and after setCustomAnimation callback
  • Trigger galaxy S4 Infrared sensor App Development?
  • How to know internal memory size in android?
  • Read & writing arrays of Parcelable objects
  • Navigation drawer - disable swipe
  • Error after adding play-services library : ':app:transformClassesWithJarMergingForDebug'
  • Android SipManager: android.net.sip.SipException: SipService.createSession() returns null
  • 5 Solutions collect form web for “backbutton in phonegap not getting registered and not working properly”

    Don’t forget to call the “deviceready” event.

    From phonegap doc :

    This is a very important event that every Cordova application should
    use.

    Cordova consists of two code bases: native and JavaScript. While the
    native code is loading, a custom loading image is displayed. However,
    JavaScript is only loaded once the DOM loads. This means your web
    application could, potentially, call a Cordova JavaScript function
    before it is loaded.

    The Cordova deviceready event fires once Cordova has fully loaded.
    After the device has fired, you can safely make calls to Cordova
    function.

    Try this

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady()
    {
        document.addEventListener("backbutton", onBackClickEvent, false);
    }
    
    function onBackClickEvent()
    {
        alert("back onBackClickEvent");     
    }
    

    I got the answer to my Question. I was struggling and many developers tries to resolve it and when i found the silly mistake i found myself so silly.

    I was writing

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

    instead of

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

    thats why i am getting an error cordova is not defined and no other listeners are getting registered.

    I faced this bug after upgrading to cordova 3.1. Turns out that this has to be explicitly added in config.xml phonegap 3.* onwards.

    <feature name="App">
            <param name="android-package" value="org.apache.cordova.App" />
        </feature>
    

    See this link

    For the back button I use this

    //Android back button handler methods
    $(document).ready(function()
    {
    document.addEventListener("deviceready", setOverrideBackbutton, false);
    });
    
    /**
    * Allow override of the back button on Android platforms
    */
    function setOverrideBackbutton()
    {
    if (typeof device != "undefined" && device.platform == "Android")
    {
        navigator.app.overrideBackbutton(true);
    }
    document.addEventListener("backbutton", backButtonTap, true);
    }
    
    /**
     * Callback after a backbutton tap on Android and windows platforms.
     * Do nothing.
     */
    function backButtonTap()
    {
    //Do not remove
    }
    

    pls check that the path and file name to your cordova.js is correct.
    make sure you have checked true cordova.jar file and Android private libs in project property-> Java Build Path-> Order & Export.

    For more infomation check this link

    and if you import more than one files then put the cordova.js file before the other.js file and it should work.

    and Just use

    <script src="cordova-2.6.0.js"></script>
    

    instead of

    and also check this link1 & link2

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.