كيفية إلغاء Asynctask عندما ينتهي النشاط؟
-
22-09-2019 - |
سؤال
في نشاطي ، أستخدم فصول Asynctask متعددة.
كيفية إلغاء Asynctask عندما ينتهي النشاط؟
نصائح أخرى
أعتقد أن أفضل مكان للقيام بذلك 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);
}
}
في مهمتي ، أتحقق من كل وقت ممكن إذا كان ذلك ممكنًا إلغاء كان يسمى
protected String doInBackground(String... arg0) {
if (this.isCancelled()) {
return null;
}
}
وبالطبع لا تنسى إسقاط البيانات التي ربما يتم إرجاعها نظرًا لعدم وجود نشاط مزيد من النشاط لاستلامها
protected void onPostExecute(List<UserStatus> result) {
if(!this.isCancelled()) {
//pass data to receiver
}
}
يتم الاحتفاظ بخيط Asynctask على قيد الحياة في تجمع الخيوط لاستقبال مستقبل Asynctask. لا يمكنك إزالتها.
لا تنتمي إلى StackOverflow