I have a collapsible SearchView
in my ActionBar
. After a search has been performed, the associated ListView
is filtered to show only matching items. During that state, the SearchView
is still expanded and shows the search string. If the user closes the SearchView
, I remove the filter.
I want to restore the search state, e.g. on configuration change or if the activity was destroyed when I return to it from another activity.
I restore the query string in onRestoreInstanceState()
, and if I find a query string in onCreateOptionsMenu()
I call
searchView.setQuery(query, true);
so as to execute the query again. It turned out that this is better than applying the query filter immediately onRestoreInstanceState()
. With the latter the list is shortly shown unfiltered, only then the query is applied again. With setQuery()
this does not happen.
Problem: The query is executed and the list is filtered, but the search view remains collapsed. Therefore the user cannot use the search view to remove the filter or to apply another query.
In onCreateOptionsMenu()
I can be sure that the search item and the search view exists, therefore I can call searchItem.expandActionView()
. Strangely, only this really expands the ActionView
- calling setIconified(false)
does not expand the view, not even when it is called twice in a row.
If I use expandActionView()
before I call setQuery()
, the SearchView
is opened and shows the text (otherwise expandActionView()
empties the SearchView
).
Unfortunately, expandActionView()
has a side effect: the suggestion list is also shown and the keyboard opens.
I can hide the keyboard using searchView.clearFocus()
. So the remaining problem is the suggestion list. How can I close an open suggestion list on a SearchView
that has text?
I wonder if there is a better way to restore a search view in the action bar that does not have so many side effects.