In this tutorial, we’ll be discussing what are DialogFragments. We’ll see how they are different from the Dialogs too with the help of a simple android application.

Android DialogFragments

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content.

Essentially a DialogFragment displays a Dialog but inside a Fragment.

Google recommends that we use DialogFragment instead of a simple Alert Dialog builder in the activity.

Why so?

  • DialogFragments have their own lifecycle methods. So the Activity is free from the responsibility of telling the Dialog what to do.
  • No more IllegalStateExceptions and leaked window crashes. This was pretty common when the activity was destroyed with the Alert Dialog still there.

Because DialogFragment is a fragment, it integrates into the activity’s lifecycle and ensures that what’s happening in the dialog window remains consistent. It’s a good practice to use DialogFragments to create dialogs in your android application.

Your class must extend DialogFragment with at least onCreateDialog and/or onCreateView implemented.

You can create Dialogs using DialogFragment in two ways:

  • onCreateDialog – Here you can create the AlertDialog using the AlertDialog.Builder class.
  • onCreateView – Here you can create a Dialog using a custom view defined.

In order to create a DialogFragment that shows a Dialog, we need to call the method show() on the DialogFragment instance as:

We can set any tag as the second argument of show().

In order to create a DialogFragment that embeds the dialog in a fragment, we just add the Fragment to the Framelayout as we do it with any Fragment.

Do you know?You can show the custom views in Fragments as well instead of just Dialogs.

When a DialogFragment class is instantiated. Methods are called in the following order:

  • onCreate
  • onCreateDialog
  • onCreateView
  • onViewCreated
  • onDestroy

Passing Data to and From the DialogFragment

In order to pass the data to the DialogFragment class, we can simply set the data using setArguments on the instance of the class.

In order to return the data from the DialogFragments to the Activity/another fragment, we need to create our custom interface.

In the following section, we’ll be creating an android application that does the following things:

  • Creates a Simple DialogFragment Dialog
  • A DialogFragment embedded in the Activity
  • DialogFragment with a style.
  • DialogFragment that returns data

Project Structure

android-dialogfragment-project

Code

The code for the activity_main.xml class is given below:

Each of the Buttons would start a different type of DialogFragment.

The xml layout for the custom view for a DialogFragment is defined in fragment_sample_dialog.xml file as shown below:

So our Dialog would show a basic Login form.

The code for the MainActivity.java is given below:

The above class implements an interface MyDialogFragment.DialogListener which triggers the method onFinishEditDialog whenever the button of the DialogFragment is clicked.
It displays the data entered in the Dialog on the Activity.

The code for the MyDialogFragment.java class is given below:

Inside onCreateDialog we create a normal AlertDialog.
dismiss() function closes the Dialog.

The output of the above application in action is given below:

android dialogfragment output

Notice that in the full screen Dialog, the data for the input field was already passed.

That brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply