Search code examples
androidandroid-search

Android: Search widget clearFocus() not working


I have a search widget on my actionbar but my problem is that when I get back from the result activity the widget is still open. I want it to return on the initial state before I clicked on the actionbar button. I tried on the onSaveInstanceState of the main activity the below code but I doesn't worked. Any ideas?

protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    searchView.setQuery("", false);
    searchView.clearFocus();
    searchView.setIconified(true);
}

Solution

  • Try the below.

    onCreateOptionsMenu(Menu menu) 
    {
         MenuItem searchItem = menu.findItem(R.id.yoursearchitem);
    }
    

    Call below when you want to close the search item.

    searchItem.collapseActionView();