1

With old appcompat we used SearchView in ActionBar and When I press Back Button I would collapse the SearchView.

But now I switched to Appcompat v21 and use ToolBar instead of it. But now SearchView does not collapse when I press Back button.

How can I solve this problem?

Best Regards.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sattar Hummatli
  • 1,360
  • 1
  • 15
  • 26
  • Solved it this way http://stackoverflow.com/questions/18924445/searchview-not-closing-correctly-on-first-back-press-its-only-loosing-focus/30561597#30561597 – powder366 May 31 '15 at 19:15

3 Answers3

1

I faced the same problem and I solved it as follow:
1- let your activity or fragment implements SearchView.OnFocusChangeListener
2- add mSearchView.setOnQueryTextFocusChangeListener(this);
3- add

@Override
 public void onFocusChange(View v, boolean hasFocus) {
    if(!hasFocus) {
         MenuItemCompat.collapseActionView(searchMenuItem);
    }
}

It works for me :)

Mohamad Shaker
  • 1,466
  • 2
  • 14
  • 21
0

Use

searchItem.collapseActionView();
Rahul
  • 479
  • 3
  • 18
-1

You can overwrite the onBackPressed() Method:

  @Override
public void onBackPressed()
{
     super.onBackPressed();  // optional depending on your needs
     finish();
}

This should do the trick.

Michael
  • 19
  • 3