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.
What I was looking for was
getSupportFragmentManager().executePendingTransactions()
as shown here. It seems the transaction wasn't in a hurry to go through. This call hurries the transaction through. The ordering of my transactions is as follows now:
06-26 10:45:43.800: I/tag(3303): displayDialog: 8
06-26 10:45:43.800: I/tag(3303): Previous Dialog Fragment is:null
06-26 10:45:43.810: I/tag(3303): displayDialog: 7
06-26 10:45:43.810: I/tag(3303): Previous Dialog Fragment is:DialogHelperFragment{40b44a78 #0 dialogHelp}
06-26 10:45:43.810: I/tag(3303): removing previous dialog
06-26 10:45:44.220: I/tag Dialog Helper(3303): Creating Dialog: 7
so Dialog type 8 is removed before it is actually created.
I hope this helps for those who get stuck on the same problem.
Edit
It seems I also had to remove addToBackStack(null)
Sorry, I didn't really look deeply at your code but I cannot find a "commit" call on your FragmentTransaction. You need to commit these transactions in the end.
EDIT: Since you are using DialogFragments which can manage the transaction on their own with show and dismiss, you should be making use of that instead.
You need to call dismiss on the Fragment's Dialog object though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With