Reputation: 89
I am trying to perform search service using Async task on changing text input in edit text as following:
et_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//my asyncTask
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
but I want to stop the current running async to perform new one on changing text
Upvotes: 3
Views: 654
Reputation: 9375
You can cancel running aynctask where you want first initialize your aynctask class to globally
private YourAsyncTask mTask;
and in your editetxt first check if your async class is not null than cancel previously class and execute again
et_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//my asyncTask
if(mtask != null){
mTask.cancel(true);
}
mTask = new YourAsyncTask().execute(et_search.getText().toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
You can call async class in onTextChanged() method
Upvotes: 2