3

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.

Mike T
  • 4,747
  • 4
  • 32
  • 52
  • Just thought it is worth to add a link to this since it is similar but not the same: http://stackoverflow.com/questions/14738572/cannot-get-dialogfragment-to-dismiss-programatically/18187157#18187157 – TacB0sS Aug 12 '13 at 12:39

2 Answers2

7

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)

Community
  • 1
  • 1
Mike T
  • 4,747
  • 4
  • 32
  • 52
1

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.

tiguchi
  • 5,392
  • 1
  • 33
  • 39
  • From the Android docs for DialogFragment.show(FragmentTransaction, String): "Display the dialog, adding the fragment using an existing transaction and then committing the transaction." It seems to me that show() commits the transaction for me. – Mike T Jun 26 '12 at 06:58
  • Yes you are right about that. I added another thought to my already posted answer while you were responding. – tiguchi Jun 26 '12 at 07:02
  • Calling dismiss() on the Dialog itself hasn't worked for other people and the general feel on other SO posts has been that calling dismiss() on the DialogFragment is better. This is what I'm doing for my alert DialogFragments but the indeterminate spinner needs to be notified when to be dismissed. How do you suggest I do that if I can't get a handle on the Fragment when getSupportFragmentManager().findFragmentByTag("dialog"); is returning null? – Mike T Jun 26 '12 at 07:06
  • 1
    First of all you could assign individual tags for these two different dialogs so you can tell them apart and retrieve and dismiss the correct one. Alternatively you could keep references to these fragments. Or you could start your server communication after the spinner dialog is visible (DialogFragment.onStart) – tiguchi Jun 26 '12 at 07:24
  • I like the idea of starting server communication after the dialog is visible. I'm struggling to get getSupportFragmentManager().findFragmentByTag("dialog"); to return anything other than null at the moment though... – Mike T Jun 26 '12 at 08:37
  • I've found a better solution. I'll post it as the answer and upvote you for being helpful. – Mike T Jun 26 '12 at 08:47