I have an activity in android that is getting data from server using AsyncTask, I'm running the task like the following:
new Task.exectue();
The problem is there are multiple calls to this AsyncTask and I want to cancel all of them when onDestroy
or onBackPressed
called, how can I achieve that? or as another soultion how can I check if there are any background tasks running in activity?
By Default AsyncTask
executes many tasks serially. That means that when the first task finishes it starts the next one and so on. What you can do is
Global Variable:
private Task myTask;
Method:
public void accessWebService(){
myTask = new Task().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "Param 1");
}
and then in onDestroy
cancel the Task.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments