Asynctask canceled during execution does not call any of the post-InBackground() methods

Asked

Viewed 114 times

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!
    }
}

1 answer

2


The big problem here is that the method onCancelled, as well as the method onPostExecute, are executed in the Thread responsible for dealing with the UI, as the documentation.

That way, when you do:

while (false == myTask.asyncTaskEstaFinalized())

in Thread UI, just after the minhaTask.cancel(false);, you don’t let the method end, preventing onCancelled be executed.

To resolve this quickly, remove this

while (false == myTask.asyncTaskEstaFinalized())

method, and do, whatever you would like to do, or within the method onAsyncTaskCancelada or within the method onPostExecute.

  • It’s true! I waver, man. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.