onQueryTextChange triggered after app resume
Found the only one solution - set listener in onResume:
@Override
public void onResume() {
super.onResume();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
fpAdapter.getFilter().filter(newText);
} else {
loadData();
}
return false;
}
}); }
use this code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(final String s) {
if(searchView.getWidth()>0)
{
// your code here
}
return false;
}
});
You can set to use the SearchView
only when it's in focus. I had a similar problem where the search was performing in my fragment every time when users resumed it. I solve it with the following method:
-Add a boolean to see when SearchView
is in focus:
//by default the SearchView isn't in focus so set it to false.
private boolean shouldSearch = false;
searchView.setOnQueryTextFocusChangeListener((view, hasFocus) -> {
if (hasFocus) {
shouldSearch = true;
} else {
shouldSearch = false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (shouldSearch) {
//do your search here
}
return true;
}
});