cancel()
method.
boolean
value and it is possible that this method may return false
. After such a “failure” call to the cancel()
method, the isCancelled()
method will also return false
.
private class Task extends AsyncTask<Object, Object, Object> { ... protected void onPostExecute(Object result) { if (!isCancelled()) { //do something interesting } } ... }
doInBackground()
method and “posts” the call to the onPostExecute()
method to the main event queue.
cancel()
method.
onPostExecute()
method has not yet been called, but it is no longer possible to cancel the AsyncTask execution - the cancel()
method will return false
. And, therefore, the method isCancelled()
on subsequent calls will also return false
.
cancel()
method (because the control from the doInBackground()
method doInBackground()
been returned) and AsyncTask “thinks” that the task has already been completed and there is nothing to cancel.
!isCancelled()
, as in the example above, is not enough.
cancel()
and isCancelled()
methods, but they are declared final in the SDK.
cancel()
method, a special flag is set that regardless of the internal state of AsyncTask, it is possible to know for sure whether the cancel()
method was called or not.Source: https://habr.com/ru/post/116309/