I recent ran across this question on StackOverflow which has no real valid response or perhaps best practice. I am pasting the question below for your convenience:
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'sshow()method.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:
- Call the Activity's
showDialog()method, passing in an int that uniquely defines what sort of Dialog you want to build. Then overrideonCreateDialog()to actually build the Dialog, and Android will display it for you.
Reasons to useDialog.show
- 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 duringonCreateDialogoronPrepareDialog.
- 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
switchstatement in the overriddenonCreateDialogmethodReasons to use
- Inside a potentially large
switchstatement in the overriddenonPrepareDialogmethodActivity.showDialog:
- The API docs for
Activity.showDialogsay that the Dialog is "managed" by the Activity which I suppose provides some benefit? But this is also true if you use theAlertDialog.Builder, I would think, because you pass inthisas an argument to the Builder's constructor.So my question is, what are the criteria for deciding when to use
- 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.
Activity.showDialogand when to useDialog.show, and why?
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment