Pergunta

Na minha atividade, uso várias classes de assíncas.

Como cancelar o AsyncTask quando a atividade terminar?

Foi útil?

Solução

Eu não entendo se o seu "cancelar" significa reversão, mas você tem um cancelar Método no ASYNCTASK classe.

Outras dicas

Eu acho que o melhor lugar para fazer isso é onStop

protected void onStop() {
    super.onStop();

    /*
    * The device may have been rotated and the activity is going to be destroyed
    * you always should be prepared to cancel your AsnycTasks before the Activity
    * which created them is going to be destroyed.
    * And dont rely on mayInteruptIfRunning
    */
    if (this.loaderTask != null) {
        this.loaderTask.cancel(false);
    }
}

Na minha tarefa, verifico o mais rápido possível se cancelar foi chamado

protected String doInBackground(String... arg0) {
    if (this.isCancelled()) {
        return null;
    }
}

E, claro, não se esqueça de soltar dados que talvez retornem, pois não há mais atividade para recebê -los

protected void onPostExecute(List<UserStatus> result) {
    if(!this.isCancelled()) {
        //pass data to receiver
    }
}

O thread de assíncas é mantido vivo em um pool de threads para futuras itens de assíncrogem. Você não pode removê -los.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top