1
When I cancel my AsyncTask
running, it does not update the flag mAsyncTaskEstaFinalizada
. Does anyone know what it might be? When I put breakpoints in the methods onCancelled()
, onCancelled(resultado)
and onPostExecute(resultado)
, Debugger does not stop at any of them. I look forward to asynctask updating the flag in a loop while()
and the app loops into an infinite loop. Can this happen? I’ve found on Logcat that no exception is being released by async task.
I’m testing on Android 4.0.3. And before anyone asks why I’m not checking the status of async task via getStatus()
, is because the states provided by this method do not meet my needs in this case.
class MinhaTask extends
AsyncTask<Void, Integer, Void> {
private boolean mAsyncTaskEstaFinalizada = false;
@Override
protected Void doInBackground(Void... params) {
int progresso;
...
publishProgress(progresso);
while (false == isCancelled()) {
...
publishProgress(progresso);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progresso) {
exibeProgresso(progresso[0]);
}
@Override
protected void onCancelled(Void resultado) { // Chamado após cancel(boolean) quando o API level é 11 ou superior.
onAsyncTaskCancelada();
}
@Override
protected void onCancelled() { // Chamado após cancel(boolean) quando o API level é 10 ou inferior.
onAsyncTaskCancelada();
}
private void onAsyncTaskCancelada() {
...
mAsyncTaskEstaFinalizada = true;
}
@Override
protected void onPostExecute(Void resultado) {
...
mAsyncTaskEstaFinalizada = true;
}
boolean asyncTaskEstaFinalizada() {
return mAsyncTaskEstaFinalizada;
}
}
Canceling the execution:
MinhaTask minhaTask = new MinhaTask();
minhaTask.execute();
...
if (minhaTask != null && minhaTask.getStatus() == AsyncTask.Status.RUNNING) {
minhaTask.cancel(false);
while (false == minhaTask.asyncTaskEstaFinalizada()) {
// Aguarda a asynctask terminar, porém está entrando em loop infinito!
}
}
It’s true! I waver, man. Thank you!
– João Victor