Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog.show() vs. Activity.showDialog()

Tags:

android

dialog

As far as I can tell, there are two ways to show a Dialog from an Activity.

  1. Create the Dialog (for example, using an AlertDialog.Builder), and then call the newly created Dialog's show() method.
  2. Call the Activity's showDialog() method, passing in an int that uniquely defines what sort of Dialog you want to build. Then override onCreateDialog() to actually build the Dialog, and Android will display it for you.

The second method seems to be the standard practice but I'm curious if there is any reason it matters which one I use. Here's all I can come up with:

Reasons to use Dialog.show

  1. If you need to parameterize the Dialog in some way, it can be a little awkward to use Activity.showDialog, as described in this question. You may have to store a String or something in a member variable, just so that it can be retrieved moments later during onCreateDialog or onPrepareDialog.
  2. The logic for creating and modifying the dialog is spread out across a number of places, potentially making the code harder to read and maintain:
    • The place where you call showDialog()
    • Inside a potentially large switch statement in the overridden onCreateDialog method
    • Inside a potentially large switch statement in the overridden onPrepareDialog method

Reasons to use Activity.showDialog:

  1. The API docs for Activity.showDialog say that the Dialog is "managed" by the Activity which I suppose provides some benefit? But this is also true if you use the AlertDialog.Builder, I would think, because you pass in this as an argument to the Builder's constructor.
  2. If your Activity is going to show the same (or a very similar) Dialog several times, this option creates it only once, instead of creating a new one each time, thus putting less strain on the system as far as allocating space for new objects, garbage collection, etc.

So my question is, what are the criteria for deciding when to use Activity.showDialog and when to use Dialog.show, and why?

like image 577
Tyler Avatar asked Jul 03 '10 04:07

Tyler


People also ask

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How many action button can a dialog have?

There should be no more than three action buttons in a dialog.

When a dialog is displayed on top of your activity what will happen?

when a dialog is shown or it's window comes visible on top of an existing activity, then it overrides partial the activity window so existing activity will move to partially invisible state and you will get call to onPause() from ActivityThread. but to be sure we also need to consider here a one think...


2 Answers

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.

like image 84
Dariusz Bacinski Avatar answered Sep 30 '22 16:09

Dariusz Bacinski


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:

final int ERROR_DIALOG = 404; final int MSG_DIALOG = 200;  .....  // if someone else read this, he/she will immediately understand what's going on showDialog( ERROR_DIALOG ); 
like image 29
Cristian Avatar answered Sep 30 '22 15:09

Cristian