The Android Developer Challenge is back! Submit your idea before December 2.

DialogFragment

open class DialogFragment : Fragment, OnCancelListener, OnDismissListener
kotlin.Any
   ↳ androidx.fragment.app.Fragment
   ↳ androidx.fragment.app.DialogFragment

Static library support version of the framework's android.app.DialogFragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Constants

static Int

Style for setStyle(int, int): a basic, normal dialog.

static Int

Style for setStyle(int, int): don't draw any frame at all; the view hierarchy returned by onCreateView is entirely responsible for drawing the dialog.

static Int

Style for setStyle(int, int): like STYLE_NO_FRAME, but also disables all input to the dialog.

static Int

Style for setStyle(int, int): don't include a title area.

Public constructors

Public methods

open Unit

Dismiss the fragment and its dialog.

open Unit

Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss().

open Dialog?

Return the Dialog this fragment is currently controlling.

open Boolean

Return the current value of setShowsDialog(boolean).

open Int

open Boolean

Return the current value of setCancelable(boolean).

open Unit
onActivityCreated(@Nullable savedInstanceState: Bundle?)

open Unit
onAttach(@NonNull context: Context)

open Unit
onCancel(@NonNull dialog: DialogInterface)

open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

open Dialog
onCreateDialog(@Nullable savedInstanceState: Bundle?)

Override to build your own custom Dialog container.

open Unit

Remove dialog.

open Unit

open Unit
onDismiss(@NonNull dialog: DialogInterface)

open LayoutInflater
onGetLayoutInflater(@Nullable savedInstanceState: Bundle?)

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

open Unit

open Unit

Dialog

Return the Dialog this fragment is currently controlling.

open Unit
setCancelable(cancelable: Boolean)

Control whether the shown Dialog is cancelable.

open Unit
setShowsDialog(showsDialog: Boolean)

Controls whether this fragment should be shown in a dialog.

open Unit
setStyle(style: Int, @StyleRes theme: Int)

Call to customize the basic appearance and behavior of the fragment's dialog.

open Unit
show(@NonNull manager: FragmentManager, @Nullable tag: String?)

Display the dialog, adding the fragment to the given FragmentManager.

open Int
show(@NonNull transaction: FragmentTransaction, @Nullable tag: String?)

Display the dialog, adding the fragment using an existing transaction and then committing the transaction.

open Unit
showNow(@NonNull manager: FragmentManager, @Nullable tag: String?)

Display the dialog, immediately adding the fragment to the given FragmentManager.

Inherited functions

Constants

STYLE_NORMAL

static val STYLE_NORMAL: Int

Style for setStyle(int, int): a basic, normal dialog.

Value: 0

STYLE_NO_FRAME

static val STYLE_NO_FRAME: Int

Style for setStyle(int, int): don't draw any frame at all; the view hierarchy returned by onCreateView is entirely responsible for drawing the dialog.

Value: 2

STYLE_NO_INPUT

static val STYLE_NO_INPUT: Int

Style for setStyle(int, int): like STYLE_NO_FRAME, but also disables all input to the dialog. The user can not touch it, and its window will not receive input focus.

Value: 3

STYLE_NO_TITLE

static val STYLE_NO_TITLE: Int

Style for setStyle(int, int): don't include a title area.

Value: 1

Public constructors

<init>

DialogFragment()

Public methods

dismiss

open fun dismiss(): Unit

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 will be popped. Otherwise, a new transaction will be committed to remove the fragment.

dismissAllowingStateLoss

open fun dismissAllowingStateLoss(): Unit

Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss(). See linked documentation for further details.

getDialog

@Nullable open fun getDialog(): Dialog?

Return the Dialog this fragment is currently controlling.

See Also

getShowsDialog

open fun getShowsDialog(): Boolean

Return the current value of setShowsDialog(boolean).

getTheme

@StyleRes open fun getTheme(): Int

isCancelable

open fun isCancelable(): Boolean

Return the current value of setCancelable(boolean).

onActivityCreated

@MainThread open fun onActivityCreated(@Nullable savedInstanceState: Bundle?): Unit

onAttach

@MainThread open fun onAttach(@NonNull context: Context): Unit

onCancel

open fun onCancel(@NonNull dialog: DialogInterface): Unit

onCreate

@MainThread open fun onCreate(@Nullable savedInstanceState: Bundle?): Unit

onCreateDialog

@MainThread @NonNull open fun onCreateDialog(@Nullable savedInstanceState: Bundle?): Dialog

Override to build your own custom Dialog container. This is typically used to show an AlertDialog instead of a generic Dialog; when doing so, onCreateView(LayoutInflater, ViewGroup, Bundle) does not need to be implemented since the AlertDialog takes care of its own content.

This method will be called after onCreate(Bundle) and before onCreateView(LayoutInflater, ViewGroup, Bundle). The default implementation simply instantiates and returns a Dialog class.

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

Parameters
savedInstanceState Bundle?: The last saved instance state of the Fragment, or null if this is a freshly created Fragment.
Return
Dialog: Return a new Dialog instance to be displayed by the Fragment.

onDestroyView

@MainThread open fun onDestroyView(): Unit

Remove dialog.

onDetach

@MainThread open fun onDetach(): Unit

onDismiss

open fun onDismiss(@NonNull dialog: DialogInterface): Unit

onGetLayoutInflater

@NonNull open fun onGetLayoutInflater(@Nullable savedInstanceState: Bundle?): LayoutInflater

onSaveInstanceState

@MainThread open fun onSaveInstanceState(@NonNull outState: Bundle): Unit

onStart

@MainThread open fun onStart(): Unit

onStop

@MainThread open fun onStop(): Unit

requireDialog

@NonNull fun requireDialog(): Dialog

Return the Dialog this fragment is currently controlling.

Exceptions
IllegalStateException if the Dialog has not yet been created (before onCreateDialog(Bundle)) or has been destroyed (after onDestroyView().

See Also

setCancelable

open fun setCancelable(cancelable: Boolean): Unit

Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this.

Parameters
cancelable Boolean: If true, the dialog is cancelable. The default is true.

setShowsDialog

open fun setShowsDialog(showsDialog: Boolean): Unit

Controls whether this fragment should be shown in a dialog. If not set, no Dialog will be created in onActivityCreated(Bundle), and the fragment's view hierarchy will thus not be added to it. This allows you to instead use it as a normal fragment (embedded inside of its activity).

This is normally set for you based on whether the fragment is associated with a container view ID passed to FragmentTransaction.add(int, Fragment). If the fragment was added with a container, setShowsDialog will be initialized to false; otherwise, it will be true.

Parameters
showsDialog Boolean: If true, the fragment will be displayed in a Dialog. If false, no Dialog will be created and the fragment's view hierarchy left undisturbed.

setStyle

open fun setStyle(style: Int, @StyleRes theme: Int): Unit

Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

Parameters
style Int: Selects a standard style: may be STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, or STYLE_NO_INPUT.
theme Int: Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you.

show

open fun show(@NonNull manager: FragmentManager, @Nullable tag: String?): Unit

Display the dialog, adding the fragment to the given FragmentManager. This is a convenience for explicitly creating a transaction, adding the fragment to it with the given tag, and committing it. This does not add the transaction to the fragment back stack. When the fragment is dismissed, a new transaction will be executed to remove it from the activity.

Parameters
manager FragmentManager: The FragmentManager this fragment will be added to.
tag FragmentManager: The tag for this fragment, as per FragmentTransaction.add.

show

open fun show(@NonNull transaction: FragmentTransaction, @Nullable tag: String?): Int

Display the dialog, adding the fragment using an existing transaction and then committing the transaction.

Parameters
transaction FragmentTransaction: An existing transaction in which to add the fragment.
tag FragmentTransaction: The tag for this fragment, as per FragmentTransaction.add.
Return
Int: Returns the identifier of the committed transaction, as per FragmentTransaction.commit().

showNow

open fun showNow(@NonNull manager: FragmentManager, @Nullable tag: String?): Unit

Display the dialog, immediately adding the fragment to the given FragmentManager. This is a convenience for explicitly creating a transaction, adding the fragment to it with the given tag, and calling FragmentTransaction#commitNow(). This does not add the transaction to the fragment back stack. When the fragment is dismissed, a new transaction will be executed to remove it from the activity.

Parameters
manager FragmentManager: The FragmentManager this fragment will be added to.
tag FragmentManager: The tag for this fragment, as per FragmentTransaction.add.