tags:

views:

64

answers:

1

Hello, I'm hoping someone can help with this, as it's driving me absolutely nuts.

I have a ProgressDialog, which tells the user my app is connecting to a server. It's launched using "showDialog(CHECKING_USER_NAME_AND_PASSWORD_AVAILABILITY);" so that the activity will manage its state for me (specifically, I want the ProgressDialog to stay visible if the device is rotated and I don't want to manage the state myself). Therefore, I am using onCreateDialog, as described here http://developer.android.com/guide/topics/ui/dialogs.html.

When the server responds, my app displays an AlertDialog by calling "showDialog(USER_NAME_AND_PASSWORD_NOT_AVAILABLE); When OK is clicked "dismissDialog(CHECKING_USER_NAME_AND_PASSWORD_AVAILABILITY);" is called to remove the ProgressDialog.

First I'll describe a normal working scenario. User clicks button, ProgressDialog appears, AlertDialog appears (note that I can still see a little bit of the ProgressDialog behind it, as expected), user clicks OK and both Dialogs disappear.

And here's a non-working scenario: User clicks button, ProgressDialog appears, AlertDialog appears, user rotates the device. NOW THIS IS WHERE IT GOES BAD - now the AlertDialog is behind the ProgressDialog and the ProgressDialog is in front. There's no way to close the ProgressDialog since it closes when the user clicks OK in the alertDialog.

I've tried moving the dismissDialog for the ProgressDialog to various places in the code (like when immedidately when the server responds), but everything I try seems to have different issues.

I stuck some toast messages in the app, and it appears the calling order of the onCreateDialog and onPrepareDialog methods gets reversed, which explains the problem. For example, when I first launch the app I see this calling order:

OnCreate for the ProgressDialog
OnPrepare for the ProgressDialog
OnCreate for the alertDialog
OnPrepare for the alertDialog

Then, when I rotate the device (with the alertDialog still up) I see:
OnCreate for the alertDialog
OnPrepare for the alertDialog
OnCreate for the ProgressDialog
OnPrepare for the ProgressDialog

Does anyone have any suggestions? I'm starting to feel like I need to use onSaveInstanceState, which sort of defeats the point of using showDialog in the first place.

A: 

What is the relationship if the dialog ID's? If you swap the values around, does that change the behavior?

Note that even if it does, I'd still advise just dismissing the ProgressDialog before showing the AlertDialog, since Android doesn't specify the order the dialogs will be re-shown in on state restore.

Walter Mundt
OK, this is interesting. Before CHECKING_USER_NAME_AND_PASSWORD_AVAILABILITY was 3 and USER_NAME_AND_PASSWORD_NOT_AVAILABLE was 2. When I rotated the device USER_NAME_AND_PASSWORD_NOT_AVAILABLE's onCreate and OnPrepare happened first. I switched the IDs as you suggested, then when I rotated the screen CHECKING_USER_NAME_AND_PASSWORD_AVAILABILITY's onCreate and OnPrepare happened first. So they appear to be happening in the order of the ID. Also, with this order the app doesn't crash when I dismiss the ProgressDialog from the AlertDialog.
As you said, I don't want to rely on this order though since we don't really know if it will always be the case.
By the way, with this current configuration (dismissing ProgressDialog when AlertDialog is created, and having the ID for the ProgressDialog lower than that of the AlertDialog) the app crashes if I rotate the device while the ProgressDialog is up.Any chance you have any more suggestions? This has been useful so far - thanks.
EDIT: saw your other comment. Yeah, don't dismiss the progress dialog in `onCreateDialog`, dismiss it at the point where your code calls `showDialog` for the alert. `onCreateDialog` will get re-invoked to rebuild any dialogs that are showing when your activity is rebooted by an orientation change.
Walter Mundt
Thanks for helping. It turns out that the main issue I was having had to do with my lack of understanding about how to dismiss the dialog properly. I've been battling these dialogs for a few days, trying to get the desired behavior in various conditions (mainly rotations occurring at different points in the sequence). The initial description of my problem didn't capture everything I was going though - it just happened to be the symptom I was seeing at the time, after having rearranged my code. I took your advice and put the dismissDialog call where I create the AlertDialog
in the onPrepareDialog, actually. As stated earlier, this was causing a crash. After debugging I realized I had two issues:(1) I was dismissing the dialog when it wasn't being shown. This was because upon rotating the device, OnCreateDialog and OnPrepareDialog were getting re-invoked (I just noticed you have a new comment above, by the way). And since I was dismissing from within OnPrepareDialog, this meant I would dismiss when the ProgressDialog wasn't being shown. I got around this issue using a flag which is set before showDialog and preserved via onSaveInstanceState.
Your suggestion of dismissing the dialog when my code calls showDialog sounds simpler than my flag solution. Thanks, I'll use that.The second issue is that my call to dismissDialog was actually referring to the previous instance of my activity sometimes. I fixed this by prefacing my calls like this:ACTIVE_INSTANCE.showDialog(DIALOG_TASKING) and this ACTIVE_INSTANCE.dismissDialog(DIALOG_TASKING);Here's the link, which explains the rest.http://groups.google.com/group/android-developers/browse_thread/thread/bf046b95cf38832d/Thanks again.
Just moved dismissDialog (for the ProgressDialog) to where my code calls showDialog (to show the AlertDialog) and it works great! Thanks again!