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!

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 Weather App (2015) Concurrency and Error Handling Handling Errors

Ganta Hemanth
Ganta Hemanth
6,460 Points

Fragments in Android

I created a class which is a extends dialog fragment. which is

public class AlertDialogFragment extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Oops sorry!").setMessage("There was an error. Please try again").setPositiveButton("OK",null);
        AlertDialog dialog = builder.create();
        return dialog;


The main activity uses the fragmet in the following code.

private void alertUser() {
        AlertDialogFragment dialog = new AlertDialogFragment();


How does the type of dialog in main activity match with the type of dialog that is being returned from the AlertDialogFragment??

How does getFragmentManager() work? What does it return??

1 Answer

Hi Ganta,

This is a fairly complex subject,

but I will try to provide a little bit of overview to answer your questions:

How does getFragmentManager() work? What does it return??

First off this question frequently arises:

Should I use getFragmentManager() vs getSupportFragmentManager()?

The answer (briefly) is:

If you are using API >= 14, then use getFragmentManager() and while using Support Package you have to use getSupportFragmentManager()

per this StackOverFlow thread:


As for an overview of some of the trickiness in using a DialogFragment here is a two part article:



There is a link at the end of part 2 that's very important (concerning avoiding Activity state loss):


After reading through those "overview articles" you should know that the Fragment Manager is not one big monolithic thing.

Instead, when dealing with the Fragment Manager,

it sometimes comes in handy to know a little bit about something called 'Fragment Transactions':


Here's a brief article about that explains a little more (and also talks about the "Backstack" toward the end):


..and I'm sure you've already glanced over the Android Dialogs page:


..as well as the Android Doc 'AlertDialog' page:


So, let's deal with some specifics though (related to your code).

You are using 'extends DialogFragment' to create an Alert type dialog box.

There is a standard (common) way to do this, which is detailed on this Android docs Class Overview page (that deals with the 'life cycle' of DialogFragments):


Of course the AlertDialog section is the one you want to look at first:


A careful reading (and understanding of all the code on this DialogFragment page)

will probably clue you in to the answer to your question:

How does the type of dialog in main activity match with the type of dialog that is being returned from the AlertDialogFragment??

Here's one article/tutorial that might be helpful to look over (since the Android docs can be a bit dry and don't always explain things much):


I also found this article helpful

(when I was designing a dialog that used multiple buttons and had to pass the data back to the Activity using 'AlertDialog.Builder' and 'setOnEditorActionListener'):


Hopefully reading through all of the above links

should help increase your understanding a little.

In regard to the use of Callbacks with AlertDialogFragment type dialogs..

Personally I don't recommend this approach

but in one of the 'MovieActivity.java' objectives from one of the Android courses it's required to pass:


The reason I don't recommend callbacks?

I have seen them sometimes used by Android newbies (that know just enough to be dangerous)

to try to communicate between different UI fragments:


As it says it says at the end --don't do this!

The right way (per this page):


Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

The page also goes on to explain how to build a Fragment to Activity (programmatic) 'interface' that allows communication (message passing) between them.

It's not the usual way to deal with AlertDialogFragments, but it's helpful in other UI situations.

One final note, though..

You haven't shown (in your code snippets above) your Android 'imports' line of code.

This is important (and could vary).

Please see Answer 1 in this StackOverFlow thread for import examples: