I am writing a simple messenger. I have this form with the dialogues: enter image description here

After I enter the name of a new interlocutor in the search and select it, I can be transferred to a new activity. The problem is that when I return to the activity with dialogs, I still have a search phrase with a login on it, the interlocutor we were looking for before. That is so: enter image description here

So the question is: how do I close / kill this search string when I return to an activity with dialogs?

// ********* I have this markup:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always" app:actionViewClass="android.support.v7.widget.SearchView" android:title="@string/search_title"/> </menu> 
  • mToolbar.getMenu (). clear (); - Tim K.
  • did not help, put onCreate event on this line - Nikola Krivosheya

2 answers 2

 private SearchView searchView; @Override public boolean onCreateOptionsMenu(Menu menu) { ... MenuItem searchMenuItem = menu.findItem(R.id.action_search); searchView = (SearchView) searchMenuItem.getActionView(); ... } @Override public void onResume() { super.onResume(); searchView.setQuery("", false); searchView.clearFocus(); searchView.setIconified(true); // Если надо не только очистить, но и свернуть } 
  • 2
    You need to add an if (searchView! = Null) check, otherwise crash, and everything works - Nikola Krivosheya
 private SearchView searchView; @Override public boolean onCreateOptionsMenu(Menu menu) { //Инициализация Вашего SearchView } @Override public void onResume() { super.onResume(); if (searchView != null && !searchView.isIconified()) { searchView.setQuery("", false); searchView.clearFocus(); searchView.setIconified(true); } } 
  • And, you can use the collapseActionView () method to close the search - Frozik6k