DevD
DevD

Reputation: 1231

Android phone hardware backbutton not firing "backbutton" event, sometimes

I am working on a phonegap (version 6+) based hybrid app. I recently noticed that on pressing the android mobile hardware back button, the phonegap "backbutton" event is not fired under the following circusmstances:

  1. input or textarea element is in focus and soft keyboard is up: In this case the hardware back button closes the soft keyboard, but neither the element looses focus nor is the "backbutton" event fired
  2. select element is open, In this case the hardware back button causes the select element to close, but the "backbutton" event is not fired and the select element does not trigger a blur or focusout event

On googling i can see that this seems to be a known issue, but, my question is: There is a workaround for the first case where i can listen to the window resize event. But for the second case i.e. the select element close, I am not sure how to capture that event? blur and focusout don't work. Is there any workaround or solution to this?

Thanks for any help.

Upvotes: 1

Views: 575

Answers (1)

Kirankumar Dafda
Kirankumar Dafda

Reputation: 2384

If you want a workaround solution for this then you can set function to check on click if select box is open or close and then if its open false set variable to true and closed then false like below.

//set variable global
var clickedDropdown;

$("select").bind("click", function () {
    var clickedDropdown = $(this);

    if (clickedDropdown.data('open') == false) {
        clickedDropdown.data('open', true);
        console.log(this.name + "   open");
        //Your code goes here
    } else {
        clickedDropdown.data('open', false);
        console.log(this.name + "   closed");
        //Your code goes here
    }
});

Updated

$(document).ready(function(){
    setTimeout(function(){
        document.addEventListener('backbutton', function(e){
            if (clickedDropdown.data('open') == true) {
                 $( "select" ).trigger( "click" );
                //or
                //You can set blur or focusout event if dropdown already open
            } else {
                //You can set another function for backbutton pressed event
            }
        });
    },500);
});

Upvotes: 1

Related Questions