I have implemented AsyncTask in my one of activity:
performBackgroundTask asyncTask = new performBackgroundTask(); asyncTask.execute();
Now, i need to implement the "Cancel" button functionality, so i have to stop the execution of the running task. I don't know how do i stop the running task(background task).
So Please suggest me, how do i cancel the AsyncTask forcefully ?
I found about the Cancel()
method of the same, but i found that calling cancel(boolean mayInterruptIfRunning)
doesn't necessarily stop the execution of the background process. All that seems to happen is that the AsyncTask will execute onCancelled(), and won't run onPostExecute() when it completes.
AsynTaskExample mAsyncTask = new AsyncTaskExample(); mAsyncTask. cancel(true);
You can either cancel the AsyncTask in the onStop method of your activity or you can let your async task finish, and not loose its progress and relink it to the next instance of your activity.
Just check isCancelled()
once in a while:
protected Object doInBackground(Object... x) { while (/* condition */) { // work... if (isCancelled()) break; } return null; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With