Search code examples
javascriptjquerybuttoncordovablock

Block Screen and Android Buttons - Phonegap


I have a function in JS that synchronyzes information, while it's downloading data I have a BlockUI js function so the user cant touch or do anything until everythings done.

The problem is that if you press the back button I get a "Aplication error" message and the app closes, how can I do to block the buttons while the synchronization function is on?

This is what I have

function addedCustomers(tx, data){
showMessage();
        try{
            $.each(data, function(i, item) {
                tx.executeSql(query);

            });
            hideMessage();
        }catch(err){
            showAlert(err);
        }
    }

The showMessage() and hideMessage() function would be the ones that blocks and unblock UI


Solution

  • You could make use of the Events for the android buttons:

    document.addEventListener("backbutton", yourCallbackFunction, false);
    document.addEventListener("menubutton", yourCallbackFunction, false);
    document.addEventListener("searchbutton", yourCallbackFunction, false);
    

    Then set a global variable such as:

    var buttonsLocked = false;
    

    In your callback methods for the button events you could add this first:

    if(buttonsLocked) {
        return false;
    }
    

    Then in your functions that you want to block/unblock just set the buttonsLocked variable to true or false accordingly.