2013-04-30 2 views
0

나는 Activity A에서 실행되는 Asynctask B를 가지고있다. B의 post 실행시, 특정 조건에서 B는 자신의 새로운 인스턴스를 생성하고 실행할 수있다.실행 중이 아닌 AsyncTask를 취소하면 onCancelled가 호출됩니까?

내가 한 일은 AsyncTask 선언을 B 내부에 전역으로두고 onCancelled B를 재정 의하여 onpostexecute에서 작성된 새 인스턴스를 취소합니다.

그래서 onDestroy of Activity A b.cancel()은 onPostExecute가 실행 된 AsyncTask를 취소합니다. 다음과 같이 currentPlansAsync의

@Override 
    protected void onCancelled() 
    { 
     if(currentPlansAsync != null) 
     { 
     currentPlansAsync.cancel(true); 
     } 
     super.onCancelled(); 
    } 

그리고 새로운 인스턴스는 onPostExecute라고 :

private void processAfterFailure() 
    { 

     if(this.counter < 3) 
     { 
     ProgressDialog progressDialog = 
      GeneralUtils.showProgressDialog(parentActivity, parentActivity.getResources().getString(string.WaitDialogTitle), 
       parentActivity.getResources().getString(string.WaitDialogMessage)); 
     this.counter++; 
     currentPlansAsync = new CurrentPlansAsync(parentActivity, application, progressDialog, application.planBL, this.counter); 
     currentPlansAsync.execute(); 

     } 
     else 
     { 
     Toast.makeText(parentActivity, parentActivity.getResources().getString(string.SERVICE_ERROR), Toast.LENGTH_LONG).show(); 
     } 

    } 

내 질문입니다 AsyncTask를 B가 실행을 중지하고 다른 AsyncTask를의 C를 실행 활동 A가들의 OnDestroy로 이동 한 경우 B가 취소됨 (b.cancel(true);) 클래스 B에서 재정의 된 onCancelled가 호출됩니까 ??

이것은 cancel() 경우, 내가 활동에서 실행 된 AsyncTask를하고 또한 AsyncTask를

+0

작업이 완료된 후에는 취소 할 수 없습니다. 하지만 당신은 onCancelled 메소드를 public으로 만들고 대신 호출 할 수 있습니다. – vorrtex

답변

3

onCancelled() 내에서 실행되는 AsyncTask를 취소 있는지 확인 doInBackground() 반환 직후라고 그래서입니다 배경 작업이 끝나기 전에 전화가 왔습니다. 그 후 cancel()AsyncTask이면 호출되지 않습니다.

나는 모든 자식 작업을 취소합니다 무엇을 할 것인가 공용 메서드를 작성하고 호출이 공용 방법을 추천 할 것입니다 : 순서 AsyncTask의의 cancel() 방법을

public void cancelChildTasks() 
{ 
    if(currentPlansAsync != null) 
    { 
     currentPlansAsync.cancel(true); 
     currentPlansAsync.cancelChildTasks(); 
    } 
} 

당신은 여전히 ​​호출해야합니다 및 처리 실행 중일 경우 doInBackground() 실행을 중지합니다. onCancelled() 대신에 공용 메서드에서 하위 작업 만 처리하면됩니다.

관련 문제