Display dialogs with DialogFragment

A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. Although you don't need to host your dialog within a fragment, doing so lets the FragmentManager manage the state of the dialog and automatically restore the dialog when a configuration change occurs.

Create a DialogFragment

To create a DialogFragment, create a class that extends DialogFragment and override onCreateDialog(), as shown in the following example.

Kotlin

class PurchaseConfirmationDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
            AlertDialog.Builder(requireContext())
                .setMessage(getString(R.string.order_confirmation))
                .setPositiveButton(getString(R.string.ok)) { _,_ -> }
                .create()

    companion object {
        const val TAG = "PurchaseConfirmationDialog"
    }
}

Java

public class PurchaseConfirmationDialogFragment extends DialogFragment {
   @NonNull
   @Override
   public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       return new AlertDialog.Builder(requireContext())
               .setMessage(getString(R.string.order_confirmation))
               .setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
               .create();
   }

   public static String TAG = "PurchaseConfirmationDialog";
}

Similar to how onCreateView() creates a root View in an ordinary fragment, onCreateDialog() creates a Dialog to display as part of the DialogFragment. The DialogFragment handles displaying the Dialog at appropriate states in the fragment's lifecycle.

As with onCreateView(), you can return any subclass of Dialog from onCreateDialog() and aren't limited to using AlertDialog.

Show the DialogFragment

You don't have to manually create a FragmentTransaction to display your DialogFragment. Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

When creating a DialogFragment from within a Fragment, use the fragment's child FragmentManager so that the state properly restores after configuration changes. A non-null tag lets you use findFragmentByTag() to retrieve the DialogFragment at a later time.

Kotlin

// From another Fragment or Activity where you wish to show this
// PurchaseConfirmationDialogFragment.
PurchaseConfirmationDialogFragment().show(
     childFragmentManager, PurchaseConfirmationDialog.TAG)

Java

// From another Fragment or Activity where you wish to show this
// PurchaseConfirmationDialogFragment.
new PurchaseConfirmationDialogFragment().show(
       getChildFragmentManager(), PurchaseConfirmationDialog.TAG);

For more control over the FragmentTransaction, you can use the show() overload that accepts an existing FragmentTransaction.

DialogFragment lifecycle

A DialogFragment follows the standard fragment lifecycle, with a few additional lifecycle callbacks. The most common ones are as follows:

  • onCreateDialog(): override this callback to provide a Dialog for the fragment to manage and display.
  • onDismiss(): override this callback if you need to perform any custom logic when your Dialog is dismissed, such as releasing resources or unsubscribing from observable resources.
  • onCancel(): override this callback if you need to perform any custom logic when your Dialog is canceled.

DialogFragment also contains methods to dismiss or set the cancelability of your DialogFragment:

  • dismiss(): dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry are popped. Otherwise, a new transaction is committed to remove the fragment.
  • setCancelable(): control whether the shown Dialog is cancelable. Use this method instead of directly calling Dialog.setCancelable(boolean).

You don't override onCreateView() or onViewCreated() when using a DialogFragment with a Dialog. Dialogs aren't only views—they have their own window. As such, it's not enough to override onCreateView(). Moreover, onViewCreated() is never called on a custom DialogFragment unless you've overridden onCreateView() and provided a non-null view.

Use custom views

You can create a DialogFragment and display a dialog by overriding onCreateView(). You can either give it a layoutId, as with a typical fragment, or use the DialogFragment constructor.

The View returned by onCreateView() is automatically added to the dialog. In most cases, this means that you don't need to override onCreateDialog(), as the default empty dialog is populated with your view.

Certain subclasses of DialogFragment, such as BottomSheetDialogFragment, embed your view in a dialog that is styled as a bottom sheet.