DialogFragment


public class DialogFragment
extends Fragment implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener

java.lang.Object
   ↳ android.app.Fragment
     ↳ android.app.DialogFragment


This class was deprecated in API level 28.
Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

Implementations should override this class and implement onCreateView(LayoutInflater,ViewGroup,Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content.

Topics covered here:

  1. Lifecycle
  2. Basic Dialog
  3. Alert Dialog
  4. Selecting Between Dialog or Embedding

Lifecycle

DialogFragment does various things to keep the fragment's lifecycle driving it, instead of the Dialog. Note that dialogs are generally autonomous entities -- they are their own window, receiving their own input events, and often deciding on their own when to disappear (by receiving a back key event or the user clicking on a button).

DialogFragment needs to ensure that what is happening with the Fragment and Dialog states remains consistent. To do this, it watches for dismiss events from the dialog and takes care of removing its own state when they happen. This means you should use show(FragmentManager,String) or