Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Adapting Data for Display in a List Handling Errors Using Dialogs

Pablo Rocha
Pablo Rocha
10,142 Points

why use AlertDialog to call the show() method?

The bottom two appear to both work the same. What would be the difference?

This is how Ben Jakuben did it in the video:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.error_title));
builder.setMessage(getString(R.string.error_message));
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show(); 

Removed the last two lines from the first code block and replaced with builder.show():

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.error_title));
builder.setMessage(getString(R.string.error_message));
builder.setPositiveButton(android.R.string.ok, null);
builder.show(); 

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Jose,

The reason the second example causes the same result is because when you call the show method on the builder it actually creates a AlertDialog with the arguments of the builder and calls the show method on the dialog.

You can find this information here in the docs.

I hope this helps.

Pablo Rocha
Pablo Rocha
10,142 Points

Makes sense. Is there a difference in performance or efficiency?