문제

My app spawns an AsyncTask which does a potentially time-consuming calculation. So, the AsyncTask unhides a progress bar:

@Override
protected void onPreExecute() {
    progressBar.setProgress(0);
    progressBar.setVisibility(View.VISIBLE);
}

(progressBar is initialized in the constructor.) The progress bar is re-hidden after the calculation is finished:

@Override
protected void onPostExecute(String result) {
    progressBar.setVisibility(View.GONE);
    // ...
}

I wanted to change the app so that several of these tasks run at once (hooray for multiple cores!). The problem is that with this design, the first AsyncTask hides the progress bar, even though there are several other tasks still running.

What's the best way to have only the last task perform an action, or to perform an action when all the tasks I called are done?

도움이 되었습니까?

해결책

I use a simple counter incremented in onPreExecute and decremented on onPostExecute. If counter reaches 0 then hide the progress bar.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top