Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing simple message dialogs

Tags:

android

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

like image 363
Mark Avatar asked Dec 18 '09 17:12

Mark


People also ask

What is a message dialogue?

Message dialogs can contain long (or short) passages of text, such as terms of use notices, user agreements for software, help text, explanations, etc.

What are the 3 types of dialogue boxes?

There are 3 types of dialog boxes: modeless, modal, and system modal. Modal dialog boxes are generally used inside a program, to display messages, and to set program parameters.

How do I get simple alert dialog on Android?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.


2 Answers

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.

like image 158
Jayesh Avatar answered Oct 09 '22 03:10

Jayesh


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.

like image 20
Erich Douglass Avatar answered Oct 09 '22 01:10

Erich Douglass