Hector
Hector

Reputation: 5438

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?

Upvotes: 2

Views: 909

Answers (2)

KunMinX
KunMinX

Reputation: 1

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();

Upvotes: 0

powder366
powder366

Reputation: 4441

Try this:

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

Upvotes: 2

Related Questions