On Android, I have implemented a class AsyncTaskWithProgress which shall show a progress dialog while the task’s background work is being performed:
public abstract class AsyncTaskWithProgress<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private final FragmentActivity activity;
private final String action;
private final String title;
private final String message;
private final int icon;
public AsyncTaskWithProgress(FragmentActivity activity, String title, String message, int icon) {
this.activity = activity;
this.title = title;
this.message = message;
this.icon = icon;
this.action = "" + hashCode();
}
@Override
protected final void onPreExecute() {
DialogFragment fragment = ProgressDialogFragment.getInstance(action, title, message, icon);
fragment.show(activity.getSupportFragmentManager(), "dialog");
actualOnPreExecute();
}
protected void actualOnPreExecute() {
}
protected final void onPostExecute(Result result) {
actualOnPostExecute(result);
context.sendBroadcast(new Intent(action));
}
protected void actualOnPostExecute(Result result) {
}
}
My ProgressDialogFagment registers a BroadcastReceiver and cancels the dialog as soon as the broadcast has been received – excerpt:
public static Dialog getDialog(Activity activity, final String action, String title, String message, int icon,
SerializableOnClickListener cancelListener) {
final ProgressDialog dialog = new ProgressDialog(activity);
dialog.setIndeterminate(true);
dialog.setOwnerActivity(activity);
dialog.setTitle(title);
dialog.setMessage(message);
if (cancelListener != null) {
dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "Cancel", cancelListener);
}
if (icon != MathUtils.NOT_AN_INT) {
dialog.setIcon(icon);
}
BroadcastReceiver dismissReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
dialog.dismiss();
context.unregisterReceiver(this);
Log.d(getClass().getSimpleName(), "Dialog dismissed");
}
};
context.registerReceiver(dismissReceiver, new IntentFilter(action));
return dialog;
}
This works in general. However, every once in a while the dialog does not get canceled, and it seems that there are orientation changes involved.
My guess is that during orientation change, the dialog fragment gets destroyed and reinstantiated. If the AsyncTask sends its “Finished!” broadcast after the fragment got destroyed and before it got reinstantiated, the fragment misses the broadcast and thus never dismisses.
Is my guess right? How to implement this in a more reliable way?
Since you’re using fragments, I would suggest instead using setRetainInstance() to make a fragment that persists across configuration changes. You could register your broadcast receiver from that fragment, show or hide the progress, etc. (Note that a fragment does not need to be visible to the user.)
Another approach that works without fragments is to add a standalone BroadcastReceiver class (registered in your manifest) for that intent, and save a value to preferences to alert your application that the broadcast was received.
In fact, you could skip the standalone BroadcastReceiver and just save a value to preferences from your AsyncTaskWithProgress that indicates the current state of the operation, e.g. Status.LOADING, Status.LOADED, Status.CANCELLED, etc. and check that value in onCreate(). However, I think using setRetainInstance() is the best approach since you’re already using fragments.