tags:

views:

261

answers:

2

Hi,

In my activity, I'd like to show simple info dialogs, stuff like:

new AlertDialog.Builder(context).setMessage(message).show();

if I do that, the dialog will leak when I rotate that phone (not to mention it will disappear as well, so the user may miss it). I can use the managed dialogs, but I'm not sure how you use it sensibly for these types of short messages? Looks like you have to do this:

showDialog(SOME_DLG_ID);
...
@Override
onCreateDialog(int id) {
    if (id == SOME_DLG_ID) {
        new AlertDialog.Builder(context).setMessage(message).show();
    }
}

there's no way to pass what the message should be into onCreateDialog since its an override method. I'd hate to make a member variable of the parent activity that just stores whatever the current message should be. How do you all do it?

Thanks

+2  A: 

You can implement Activity.onPrepareDialog(int, Dialog) to switch out the message before the dialog is shown on the screen. So you could do something like:

@Override protected void onPrepareDialog(int id, Dialog dialog) {
    if (id == SOME_DLG_ID) {
        ((AlertDialog) dialog).setMessage(message);
    }
}

You'd still have to keep track of the message you're current showing in your activity, but at least this way, you're not creating a Dialog object for each message you want to show.

Erich Douglass
Well the problem here is the same, you can't pass the string message you have at the point you call showDialog() to onPrepareDialog or onCreateDialog()...
ruibm
Ah, my bad... I suppose I misunderstood the question.
Erich Douglass
Yeah and this is the main problem really, you need this weird pattern to get the dynamic message string to the dialog - it feels really awkward! Thanks
Mark
+2  A: 

if I do that, the dialog will leak when I rotate that phone (not to mention it will disappear as well, so the user may miss it)

You can add

<activity 
    android:configChanges="orientation|keyboardHidden"
>

to your AndroidManifest.xml to prevent restarting the activity when the phone rotates. I am using it in my app and my AlertDialog survives the rotation of phone.

Jayesh