JS Prompt to exit app not working

Hi again guys. Still having problem with PGB migration to volt. Now the app exit prompt JS is not working on volt like it used to work on Phonegap Build.
Here´s the code, hope you can help us:

<script>  
// Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    // device APIs are available
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    // Handle the back button
    //
    function onBackKeyDown() {
    navigator.notification.confirm(
            'Are you sure you want to exit?!', // message
             onConfirm,            // callback to invoke with index of button pressed
            'Confirm exit',           // title
            ['Yes','No']     // buttonLabels
        );
        function onConfirm(buttonIndex) {
            if (buttonIndex == 1) {
                navigator.app.exitApp();
            }
        }
    }  
</script>

I don’t know anything about this specific problem, but have you hooked up the Chrome Remote Debugger to see if there are any messages on the console?

Yes but nothing shown :frowning:

By the time the onload event has fired you’ve probably missed the deviceready event.
(I used to have a .js file that loaded and logged all the events when they fired and it was quite the eye opener.)

What I would do is to remove the onload event and just put this code in your index.html head

<script>
document.addEventListener("deviceready", onDeviceReady, false);
</script>

Thank you Phil. Your idea has sense, but I made those changes and still doesn´t work. Thanks for trying to help. Any other ideas are welcome.

Perhaps it would help if you gave a bit more information than “it doesn’t work the same as it did on PhoneGap”.

Keep in mind that VoltBuilder uses the latest Cordova libraries. Not everything will work the same as under PhoneGap as a result.

ok I understand what you mean. I meant that back button function worked fine after building it in pgb, which means it opened the popup prompting user to exit app, and now, after building using volt it just has the default back button behavior, which sends the app to background mode.
Code is pure javascript, did not change at all from one version to the other, but there must be something overriding regular behavior.

If the back button isn’t working, it sounds like there are one of three possible problems:

  1. the code is not getting loaded therefore the back key press is not getting detected.
  2. the event you’re trapping (backbutton) has changed.
  3. the key action you’re trapping (onBackKeyDown) has changed.

I’d add an alert (or console.log if you’re debugging via ADB) inside the onDeviceReady() event listener.

function onDeviceReady() {
  // Register the event listener
  console.log("Inside the onDeviceReady event handler");
  document.addEventListener("backbutton", onBackKeyDown, false);
}

If the message gets logged to the console then your device listener is getting loaded and the problem is either number 2 or 3.

(I’m betting on #1 or #3)

Thank you Phil, pretty clear as always!
I tried to test this but volt builder service says it is building for iOS despite having uploaded an android version (and of course pressing android button in placeholder builder page). Hope this can be fixed soon :slight_smile:

Building for Android is working fine. Check your voltbuilder.json file. What is platform set to?

I do notice the title on the upload screen is incorrect. Ignore that - the correct build is still being done. (We’ll fix that!)

BTW, I read somewhere that we’re not supposed to use keydown events anymore but instead use the keyup events. I wonder if you’ve hit that issue?

Hi Phil, thanks for your help. First of all, the backKeyDown is just a name for the function, the trigger is called backbutton and don´t really know how to setup whether to apply it when it is down or up again.
Regarding options, the alert didn´t show up. Therefore, i´ve set just a body onload=“load()” function and add the backbutton event listener in there. Then, the alert did show up, but the back button event listener didn´t work as the back button acted as default. I´m really confused!!!

SOLVED !!

The solution is to insert

<script type = "text / javascript" charset = "utf-8" src = "cordova.js"> </script>

not only on index.html but all pages you trigger cordova