Thanks to Code Droid, I was able to write an abstract AsyncTaskclass that shows an indefinite progress bar after a specified delay. Just add this class instead AsyncTaskand be sure to call super()if necessary:
public abstract class AsyncTaskWithDelayedIndeterminateProgress
<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private static final int MIN_DELAY = 250;
private final ProgressDialog progressDialog;
private final CountDownTimer countDownTimer;
protected AsyncTaskWithDelayedIndeterminateProgress(Activity activity) {
progressDialog = createProgressDialog(activity);
countDownTimer = createCountDownTimer();
}
@Override protected void onPreExecute() {
countDownTimer.start();
}
@Override protected void onPostExecute(Result children) {
countDownTimer.cancel();
if(progressDialog.isShowing())
progressDialog.dismiss();
}
private ProgressDialog createProgressDialog(Activity activity) {
final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setIndeterminate(true);
return progressDialog;
}
private CountDownTimer createCountDownTimer() {
return new CountDownTimer(MIN_DELAY, MIN_DELAY + 1) {
@Override public void onTick(long millisUntilFinished) { }
@Override public void onFinish() {
progressDialog.show();
}
};
}
source
share