Search code examples
androideclipsesearchview

How to clear text from search menu android


I've created a search in my application. Also database and contant provider. When I click on the term in the search, then starts an activity that leads to explanation of this term. When I go back to the previous activity, the text is still visible in the search box. I'd like to be automatically cleared.

I was looking for solution to the problem, but I have not found it. Please help!

SearchableDictionary.java

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchable_dictionary, menu);

        MenuItem item = menu.findItem(R.id.search);

        SearchView searchView = SearchView)MenuItemCompat.getActionView(item);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(info); 
        searchView.setIconified(true);

I tried with: searchView.clearFocus(); searchView.setQuery("", false); but it did not work for me.


Solution

  • onCreateOptionsMenu will most likely not be called when you go back to the previous activity as unless there are memory constraints the activity will not be killed and restarted.

    I would recommend calling searchView.setQuery("", false) in the callback when the user clicks on a result. Then start the new Activity and when the user returns the search field will already have been cleared.