How can I get onBackPressed() while SearchView is activated?

another way is to listen on the MenuItem for the ActionExpand/Collapse :

    MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
    searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do whatever you need
            return true; // KEEP IT TO TRUE OR IT DOESN'T OPEN !!
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do whatever you need
            return true; // OR FALSE IF YOU DIDN'T WANT IT TO CLOSE!
        }
    });
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    ... // Keep doing as you do

I think this way is cleaner, as you listen directly on what you want

Thanks to this thread


for supporting older devices, following code can be used:

        MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                //Do whatever you want                   
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                //Do whatever you want
                return true;
            }
        });