I'm using Android Support Library (v4) and ActionBarSherlock. I'm trying to close a progress dialog programatically. I've coded a small utility class to help with dialog management.
The dialog is shown from an AsyncTask.onPreExecute
. It gets displayed correctly. Then I fire a config change by rotating the device, which destroys the activity (onDestroy calls AsyncTask.cancel(true)
). AsyncTask.onCancelled
is called, and is in this method where I'm trying to close the dialog. But nothing happens. Here are the helper functions to show and close the dialog:
public abstract class DialogHelperActivity extends SherlockFragmentActivity {
protected void showProgressDialog(final String msg, final String tag){
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
DialogFragment dialogFragment = ProgressDialogFragment.newInstance(msg);
ft.add(dialogFragment, tag);
ft.disallowAddToBackStack();
ft.commitAllowingStateLoss(); //If I try with regular commit(), exceptions are thrown.
}
protected void closeDialog(final String tag){
FragmentManager fm = this.getSupportFragmentManager();
Fragment dialogFragment = fm.findFragmentByTag(tag);
if(dialogFragment != null){
FragmentTransaction ft = fm.beginTransaction();
ft.remove(dialogFragment);
ft.commitAllowingStateLoss();
} else {
System.err.println("dialog not found!"); //This line is hit always
}
}
public static class ProgressDialogFragment extends SherlockDialogFragment {
static ProgressDialogFragment newInstance(final String msg) {
ProgressDialogFragment adf = new ProgressDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("alert-message", msg);
adf.setArguments(bundle);
return adf;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setCancelable(false);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style,theme);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
String message = bundle.getString("alert-message");
ProgressDialog dialog = new ProgressDialog(getActivity());
if(message != null){
dialog.setMessage(message);
}
dialog.setCancelable(false);
dialog.setIndeterminate(true);
return dialog;
}
}
}
After rotating the device, the AsyncTask is cancelled. I'm calling closeDielog
from onPostExecute
and also from onCancelled
. The dialog never gets closed because the tag ID is not found (findFragmentByTag
returns null). I'm puzzled with this. The tag is a static String in my implementation activity so there's no chance of it being lost or changed between the calls to showProgressDialog
and closeDialog
.
Any idea/hint/suggestion will be much appreciated.
Thanks.
The problem is that I'm cancelling the
AsyncTask
in the activity'sonDestroy
. This is ok to get rid of the bg thread, butAsyncTask.onCancelled
is no place to close a fragment, because it runs AFTER the activity has been destroyed. Before that, a new activity is created, and the fragment manager restores a new dialog (even if it was created withsetRetainInstance(false)
, which I guess is the default).The timeline of calls is something like this:
onDestroy
, cancels the asynctask.onDetach
.onCancel
executes, callscloseDialog
, but the tag is not found.My error was assuming the string tag identified a fragment globally in the application context, but it turns out that the actual fragment ID assigned by the fragment manager is a combination of fragment tag/id and its activity id. When the activity is destroyed, their fragments are detached, and after this point, even if a new fragment with the same tag/id is in the foreground, as it is attached to a different activity, the fragment manager returns null when the old activity calls findFragmentByTag.
However this tag/id is enough for the new fragment to be passed the arguments bundle of the old fragment. This duality is confusing, but it also enables a hack: We can populate the arguments bundle of the fragment in its
onStop
callback with a "cancelled" flag, an query about it in theonResume
callback, where it calls dismiss itself if the flag is found. That way I can have a progress dialog that conceptually belongs to the AsyncTask, and dies with it.