Skip to content Skip to sidebar Skip to footer

Dialog.show() Vs. Activity.showdialog()

As far as I can tell, there are two ways to show a Dialog from an Activity. Create the Dialog (for example, using an AlertDialog.Builder), and then call the newly created Dialog's

Solution 1:

In my opinion you should prefer showDialog because this method will do most of the work for you. In example You don't have to worry that you will lose reference to your dialog after changing screen orientation. It will be recreated automatically. Dialog.show is much more prone to errors.

So I suggest you to use showDialog everywhere you can.

Solution 2:

I think the decision is up to you, depending on the good reasons you have pointed out. If I have an activity which has just one dialog (say, for displaying an alert) I'd just fire the Dialog.show method... on the other hand, if the UI of my app relies too much of its functionality on the use of dialogs it'd be much better to use showDialog(). I think the best advantage of using showDialog() is that it's easier to read:

finalint ERROR_DIALOG = 404;
finalint MSG_DIALOG = 200;

.....

// if someone else read this, he/she will immediately understand what's going onshowDialog( ERROR_DIALOG );

Solution 3:

You can use the overloaded method showDialog(int, Bundle) introduced in API level 8. Simple shove the message to be shown into the bundle, which will be available in onPrepareDialog(int, Dialog, Bundle).

Yes, I know that showDialog() itself has been deprecated now.

Post a Comment for "Dialog.show() Vs. Activity.showdialog()"