I've looked around for a solution but it seems this is not a common problem.
I want to have an indeterminate dialog spinner while my app connects to a server then clear that dialog and display a different dialog when the request completes. I'm using the Fragment
compatibility package. The problem is that the spinner is not being removed before the 2nd dialog is displayed.
Here is my code that shows the dialogs, and should remove any current dialogs:
void displayDialog(int type, String message) {
Log.i(logTag, "displayDialog: " + type);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
Log.i(logTag, "removing previous dialog");
ft.remove(prev); //TODO maybe use ((DialogFragment)dialog).dismiss(); ?
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = DialogHelperFragment.newInstance(type, message);
newFragment.show(ft, "dialog");
}
Here is the calling code that I'm using to troubleshoot this bug:
displayDialog(DialogHelperFragment.DIALOG_PROGRESS, null);
displayDialog(DialogHelperFragment.DIALOG_PURCHASE_SUCCESS, null);
Here's my corresponding LogCat output :
06-25 13:53:35.497: I/tag(11008): displayDialog: 8
06-25 13:53:35.497: I/tag(11008): displayDialog: 7
06-25 13:53:35.897: I/tag Dialog Helper(11008): Creating Dialog: 8
06-25 13:53:35.907: I/tag Dialog Helper(11008): Creating Dialog: 7
The problem is that
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
returns null because the first dialog hasn't been created or attached by the time displayDialog() is called again.
Any tips would be super helpful.