سؤال

في نشاطي ، أستخدم فصول Asynctask متعددة.

كيفية إلغاء 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. لا يمكنك إزالتها.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top