Вопрос

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