Why would one use the AlertDialog.Builder
class rather than the methods directly available to AlertDialog
,
For example, why use AlertDialog.Builder.setCancellable
rather than AlertDialog.setCancellable
? Surely this is a case of redundancy?
Because AlertDialog.setCancellable
returns void and AlertDialog.Builder.setCancellable
returns an AlertDialog.Builder
.
This means that the builder allows you to chain a bunch of settings with a little less verbosity. It's just a convenience class
AlertDialog allows you to show a dialog above your activity/fragment. It is usually used for prompting user for interaction including few buttons or notifying for something.
AlertDialog.Builder is an inner static class of AlertDialog which allows you to quickly setup a dialog with its handy methods. Its just like a helper class to AlertDialog. It is used for calling methods in a chain.
AlertDialog
) to be immutable/abstract while the factory counterparts are not which can tremendously ease the process of creating the product. take for example StringBuilder
or ProcessBuilder
- andr 2013-02-04 02:26
AlertDialog.Builder
does the settings of the attributes such as setTitle()
or setMessage()
and are not displayed to the user.
AlertDialog
is the one that displays those attributes which have been set in the AlertDialog.Builder
.
The purpose of having both as mentioned somewhere is that it allows settings to be made separately from the actual displaying which in turn makes thing convenient.
I think that factory methods are just more convinient.