Igor Lobanov Igor Lobanov - 1 month ago 13
Java Question

The onQueryTextSubmit in SearchView is processed twice in Android Java

Why the

onQueryTextSubmit
method in
SearchView
is processed twice?
I need one result, how can I do it?

This is my code:

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (query != null)
audioRequest(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}

Answer

You can use the following code to prevent onQueryTextSubmit from getting executed twice:

searchView.clearFocus();

Comments