I have an Activity
A which has a button. Up on tapping on the button, it opens up another Activity
B, which displays data fetched from server (http). The API
version that I am using is 15.
As soon as the Activity
B is opened, in onCreate()
method, I am calling following lines of code.
asyncFetch = new AsyncFetch();
asyncFetch.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
This Async
task performs http
communication to fetch data from server.
@Override
protected Object doInBackground(Object... params) {
...
defaultHttpClient = new DefaultHttpClient();
httpPost = new HttpPost(theURL);
...
HttpResponse response = defaultHttpClient.execute(httpPost);
...
return response...;
}
Once data is fetched, the UI is populated.
onBackPressed()
and home
button, I am calling the following code to cancel the Async
task.
asyncFetch.cancel(true);
When a back button or home button is tapped, the Async
task is not getting cancelled and UI waits for the Async task to complete and then it goes back to parent Activity.
The reason of not using the execute()
is because I have couple of Async
tasks that are running which I don't want this task to wait for other Async
task to complete.
Can someone tell me how to cancel this Async task? Any help is appreciated.