androidandroid-actionbarandroid-searchmanagerandroid-search

Why doesnt collapseActionView() close SearchView


I have an Android application that employs an ActionBar SearchView
Everything works fine when requesting a search, the search icon expands to allow text entry, the softkeyboard appears, i enter a query string and this is passed to my designated SearchActivity to display the search results.
The problem is that on pressing the back button and returning to the original activity the SearchView is still expanded, and still contains my search query. I have attempted to call collapseActionView() on the searchMenuItem however this doesn't work.
When i test the menuItem for isActionViewExpanded it returns false.
how do i get the desired behaviour?


Solution

  • Try this:

    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
    sv.onActionViewCollapsed();