stephenfin
stephenfin

Reputation: 1467

AlertDialog vs AlertDialog.Builder

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?

Upvotes: 10

Views: 6392

Answers (5)

Andy Marchewka
Andy Marchewka

Reputation: 442

Building on jqpublic answer, more than a convenience, a builder lets you ensure the dialog doesn't exist in an unexpected or inappropriate state. In general, a builder let's you take all the code you need to collect values, then instantiate the object only when you're ready.

Upvotes: 0

user1977094
user1977094

Reputation: 15

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.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

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.

Upvotes: 10

jqpubliq
jqpubliq

Reputation: 11904

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

Upvotes: 8

user468311
user468311

Reputation:

I think that factory methods are just more convinient.

Upvotes: 0

Related Questions