Вопрос

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?

Это было полезно?

Решение

Try this:

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

Другие советы

The real reason is that there is an extra toolbar inside.

When back pressed, another toolbar instance is used, and the member is null, resulting in unsuccessful subsequent callbacks.

So the solution is very simple. You can directly call the collapseActionView method of the toolbar of the current activity.

mToolbar.collapseActionView();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top