Come annullare AsyncTask quando finisce di attività?
-
22-09-2019 - |
Domanda
Nella mia attività io uso più classi AsyncTask.
Come annullare AsyncTask quando finisce attività?
Altri suggerimenti
Credo che il posto migliore per farlo è 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);
}
}
nel mio compito che poi controllare il più spesso possibile se Annulla è stato chiamato
protected String doInBackground(String... arg0) {
if (this.isCancelled()) {
return null;
}
}
e, naturalmente, non dimenticate di rilasciare i dati che forse restituito poiché non c'è più attività di riceverlo
protected void onPostExecute(List<UserStatus> result) {
if(!this.isCancelled()) {
//pass data to receiver
}
}
Il filo AsyncTask è mantenuto in vita in un pool di thread per le future istanze di AsyncTask. Non è possibile rimuoverli.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow