문제

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