{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

DialogFragment

{% setvar page_path %}androidx/fragment/app/DialogFragment.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_java_switcher2.md" %}

public class DialogFragment extends Fragment

Object
   ↳ Fragment
     ↳ 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 final @NonNull int

Style for setStyle: a basic, normal dialog.

static final @NonNull int

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

static final @NonNull int

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

static final @NonNull int

Style for setStyle: don't include a title area.

Public constructors

Constructor used by the default FragmentFactory.

DialogFragment(@NonNull int contentLayoutId)

Alternate constructor that can be called from your default, no argument constructor to provide a default layout that will be inflated by onCreateView.

Public methods

@NonNull void

Dismiss the fragment and its dialog.

@NonNull void

Version of dismiss that uses FragmentTransaction.commitAllowingStateLoss().

@Nullable Dialog

Return the Dialog this fragment is currently controlling.

@NonNull boolean

Return the current value of setShowsDialog.

@NonNull int
@NonNull boolean

Return the current value of setCancelable.

@NonNull void
onAttach(@NonNull Context context)

Called when a fragment is first attached to its context.

@NonNull void
onCancel(@NonNull DialogInterface dialog)
@NonNull void
onCreate(@Nullable Bundle savedInstanceState)

Called to do initial creation of a fragment.

@NonNull Dialog
onCreateDialog(@Nullable Bundle savedInstanceState)

Override to build your own custom Dialog container.

@NonNull void

Remove dialog.

@NonNull void

Called when the fragment is no longer attached to its activity.

@NonNull void
onDismiss(@NonNull DialogInterface dialog)
@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)

Returns the LayoutInflater used to inflate Views of this Fragment.

@NonNull void
onSaveInstanceState(@NonNull Bundle outState)

Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted.

@NonNull void

Called when the Fragment is visible to the user.

@NonNull void

Called when the Fragment is no longer started.

@NonNull void
onViewStateRestored(@Nullable Bundle savedInstanceState)

Called when all saved state has been restored into the view hierarchy of the fragment.

final @NonNull Dialog

Return the Dialog this fragment is currently controlling.

@NonNull void
setCancelable(@NonNull boolean cancelable)

Control whether the shown Dialog is cancelable.

@NonNull void
setShowsDialog(@NonNull boolean showsDialog)

Controls whether this fragment should be shown in a dialog.

@NonNull void
setStyle(@NonNull int style, @NonNull int theme)

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

@NonNull void

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

@NonNull int

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

@NonNull void

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

Inherited methods

From class Fragment
@NonNull void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)

Print the Fragments's state into the given stream.

final @NonNull boolean

Subclasses can not override equals().

final @Nullable FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

@NonNull boolean

Returns whether the the exit transition and enter transition overlap or not.

@NonNull boolean

Returns whether the the return transition and reenter transition overlap or not.

final @Nullable Bundle

Return the arguments supplied when the fragment was instantiated, if any.

final @NonNull FragmentManager

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

@Nullable Context

Return the Context this fragment is currently associated with.

@NonNull Factory

The Fragment's arguments when this is first called will be used as the defaults to any androidx.lifecycle.SavedStateHandle passed to a view model created using this factory.

@Nullable Object

Returns the Transition that will be used to move Views into the initial scene.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.

final @Nullable FragmentManager

This method is deprecated.

This has been removed in favor of getParentFragmentManager() which throws an IllegalStateException if the FragmentManager is null.

final @Nullable Object

Return the host object of this fragment.

final @NonNull int

Return the identifier this fragment is known by.

final @NonNull LayoutInflater

Returns the cached LayoutInflater used to inflate Views of this Fragment.

@NonNull Lifecycle

Overriding this method is no longer supported and this method will be made final in a future version of Fragment.

@NonNull LoaderManager

This method is deprecated.

Use LoaderManager.getInstance(this).

final @Nullable Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull FragmentManager

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

@Nullable Object

Returns the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

final @NonNull Resources

Return requireActivity().getResources().

final @NonNull boolean

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.

final @NonNull SavedStateRegistry
@Nullable Object

Returns the Transition that will be used for shared elements transferred into the content Scene.

@Nullable Object

Return the Transition that will be used for shared elements transferred back during a pop of the back stack.

final @NonNull String
getString(@NonNull int resId)

Return a localized string from the application's package's default string table.

final @NonNull String
getString(@NonNull int resId, @Nullable Array<@NonNull Object> formatArgs)

Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and format.

final @Nullable String

Get the tag name of the fragment, if specified.

final @Nullable Fragment

This method is deprecated.

Instead of using a target fragment to pass results, use FragmentManager#setFragmentResult(String, Bundle) to deliver results to FragmentResultListener instances registered by other fragments via FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener).

final @NonNull int

This method is deprecated.

When using the target fragment replacement of FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener) and FragmentManager#setFragmentResult(String, Bundle), consider using setArguments to pass a {@code requestKey} if you need to support dynamic request keys.

final @NonNull CharSequence
getText(@NonNull int resId)

Return a localized, styled CharSequence from the application's package's default string table.

@NonNull boolean

This method is deprecated.

Use FragmentTransaction#setMaxLifecycle(Fragment, Lifecycle.State) instead.

@Nullable View

Get the root view for the fragment's layout (the one returned by onCreateView), if provided.

@NonNull LifecycleOwner

Get a LifecycleOwner that represents the Fragment's View lifecycle.

@NonNull LiveData<LifecycleOwner>

Retrieve a LiveData which allows you to observe the lifecycle of the Fragment's View.

@NonNull ViewModelStore

Returns the ViewModelStore associated with this Fragment

final @NonNull int

Subclasses can not override hashCode().

static @NonNull Fragment
instantiate(@NonNull Context context, @NonNull String fname)

This method is deprecated.

Use FragmentManager#getFragmentFactory() and FragmentFactory#instantiate(ClassLoader, String)

static @NonNull Fragment
instantiate(
    @NonNull Context context,
    @NonNull String fname,
    @Nullable Bundle args
)

This method is deprecated.

Use FragmentManager#getFragmentFactory() and FragmentFactory#instantiate(ClassLoader, String), manually calling setArguments on the returned Fragment.

final @NonNull boolean

Return true if the fragment is currently added to its activity.

final @NonNull boolean

Return true if the fragment has been explicitly detached from the UI.

final @NonNull boolean

Return true if the fragment has been hidden.

final @NonNull boolean

Return true if the layout is included as part of an activity view hierarchy via the <fragment> tag.

final @NonNull boolean

Return true if this fragment is currently being removed from its activity.

final @NonNull boolean

Return true if the fragment is in the resumed state.

final @NonNull boolean

Returns true if this fragment is added and its state has already been saved by its host.

final @NonNull boolean

Return true if the fragment is currently visible to the user.

@NonNull void
onActivityCreated(@Nullable Bundle savedInstanceState)

This method is deprecated.

use onViewCreated for code touching the Fragment's view and onCreate for other initialization.

@NonNull void
onActivityResult(
    @NonNull int requestCode,
    @NonNull int resultCode,
    @Nullable Intent data
)

This method is deprecated.

use registerForActivityResult with the appropriate ActivityResultContract and handling the result in the callback.

@NonNull void
onAttach(@NonNull Activity activity)

This method is deprecated.

See onAttach.

@NonNull void

This method is deprecated.

The responsibility for listening for fragments being attached has been moved to FragmentManager.

@NonNull void
onConfigurationChanged(@NonNull Configuration newConfig)
@NonNull boolean
onContextItemSelected(@NonNull MenuItem item)

This hook is called whenever an item in a context menu is selected.

@Nullable Animation
onCreateAnimation(
    @NonNull int transit,
    @NonNull boolean enter,
    @NonNull int nextAnim
)

Called when a fragment loads an animation.

@Nullable Animator
onCreateAnimator(
    @NonNull int transit,
    @NonNull boolean enter,
    @NonNull int nextAnim
)

Called when a fragment loads an animator.

@NonNull void
onCreateContextMenu(
    @NonNull ContextMenu menu,
    @NonNull View v,
    @Nullable ContextMenuInfo menuInfo
)

Called when a context menu for the {@code view} is about to be shown.

@NonNull void
onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater)

Initialize the contents of the Fragment host's standard options menu.

@Nullable View
onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)

Called to have the fragment instantiate its user interface view.

@NonNull void

Called when the fragment is no longer in use.

@NonNull void

Called when this fragment's option menu items are no longer being included in the overall options menu.

@NonNull void
onHiddenChanged(@NonNull boolean hidden)

Called when the hidden state (as returned by isHidden of the fragment has changed.

@NonNull void
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

@NonNull void
onInflate(
    @NonNull Activity activity,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated.

See onInflate.

@NonNull void
@NonNull void
onMultiWindowModeChanged(@NonNull boolean isInMultiWindowMode)

Called when the Fragment's activity changes from fullscreen mode to multi-window mode and visa-versa.

@NonNull boolean
onOptionsItemSelected(@NonNull MenuItem item)

This hook is called whenever an item in your options menu is selected.

@NonNull void

This hook is called whenever the options menu is being closed (either by the user canceling the menu with the back/menu button, or when an item is selected).

@NonNull void

Called when the Fragment is no longer resumed.

@NonNull void
onPictureInPictureModeChanged(@NonNull boolean isInPictureInPictureMode)

Called by the system when the activity changes to and from picture-in-picture mode.

@NonNull void

Prepare the Fragment host's standard options menu to be displayed.

@NonNull void
onPrimaryNavigationFragmentChanged(
    @NonNull boolean isPrimaryNavigationFragment
)

Callback for when the primary navigation state of this Fragment has changed.

@NonNull void
onRequestPermissionsResult(
    @NonNull int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull Array<@NonNull int> grantResults
)

This method is deprecated.

use registerForActivityResult passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

@NonNull void

Called when the fragment is visible to the user and actively running.

@NonNull void
onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

Called immediately after onCreateView has returned, but before any saved state has been restored in to the view.

@NonNull void

Postpone the entering Fragment transition until startPostponedEnterTransition or FragmentManager#executePendingTransactions() has been called.

final @NonNull void
postponeEnterTransition(@NonNull long duration, @NonNull TimeUnit timeUnit)

Postpone the entering Fragment transition for a given amount of time and then call startPostponedEnterTransition.

final @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultCallback<O> callback
)

If the host of this fragment is an ActivityResultRegistryOwner the ActivityResultRegistry of the host will be used.

final @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<O> callback
)
@NonNull void

Registers a context menu to be shown for the given view (multiple views can show the context menu).

final @NonNull void
requestPermissions(
    @NonNull Array<@NonNull String> permissions,
    @NonNull int requestCode
)

This method is deprecated.

use registerForActivityResult passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

final @NonNull FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

final @NonNull Bundle

Return the arguments supplied when the fragment was instantiated.

final @NonNull Context

Return the Context this fragment is currently associated with.

final @NonNull FragmentManager

This method is deprecated.

This has been renamed to getParentFragmentManager() to make it clear that you are accessing the FragmentManager that contains this Fragment and not the FragmentManager associated with child Fragments.

final @NonNull Object

Return the host object of this fragment.

final @NonNull Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull View

Get the root view for the fragment's layout (the one returned by onCreateView).

@NonNull void

Sets whether the the exit transition and enter transition overlap or not.

@NonNull void

Sets whether the the return transition and reenter transition overlap or not.

@NonNull void
setArguments(@Nullable Bundle args)

Supply the construction arguments for this fragment.

@NonNull void
setEnterSharedElementCallback(@Nullable SharedElementCallback callback)

When custom transitions are used with Fragments, the enter transition callback is called when this Fragment is attached or detached when not popping the back stack.

@NonNull void

Sets the Transition that will be used to move Views into the initial scene.

@NonNull void
setExitSharedElementCallback(@Nullable SharedElementCallback callback)

When custom transitions are used with Fragments, the exit transition callback is called when this Fragment is attached or detached when popping the back stack.

@NonNull void

Sets the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.

@NonNull void
setHasOptionsMenu(@NonNull boolean hasMenu)

Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu and related methods.

@NonNull void

Set the initial saved state that this Fragment should restore itself from when first being constructed, as returned by FragmentManager.saveFragmentInstanceState.

@NonNull void
setMenuVisibility(@NonNull boolean menuVisible)

Set a hint for whether this fragment's menu should be visible.

@NonNull void

Sets the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

@NonNull void
setRetainInstance(@NonNull boolean retain)

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@NonNull void

Sets the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.

@NonNull void

Sets the Transition that will be used for shared elements transferred into the content Scene.

@NonNull void

Sets the Transition that will be used for shared elements transferred back during a pop of the back stack.

@NonNull void
setTargetFragment(@Nullable Fragment fragment, @NonNull int requestCode)

This method is deprecated.

Instead of using a target fragment to pass results, the fragment requesting a result should use FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener) to register a FragmentResultListener with a {@code * requestKey} using its parent fragment manager.

@NonNull void
setUserVisibleHint(@NonNull boolean isVisibleToUser)

This method is deprecated.

If you are manually calling this method, use FragmentTransaction#setMaxLifecycle(Fragment, Lifecycle.State) instead.

@NonNull boolean

Gets whether you should show UI with rationale before requesting a permission.

@NonNull void
startActivity(@NonNull Intent intent)

Call Activity#startActivity(Intent) from the fragment's containing Activity.

@NonNull void
startActivity(@NonNull Intent intent, @Nullable Bundle options)

Call Activity#startActivity(Intent, Bundle) from the fragment's containing Activity.

@NonNull void
startActivityForResult(@NonNull Intent intent, @NonNull int requestCode)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

@NonNull void
startActivityForResult(
    @NonNull Intent intent,
    @NonNull int requestCode,
    @Nullable Bundle options
)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

@NonNull void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    @NonNull int requestCode,
    @Nullable Intent fillInIntent,
    @NonNull int flagsMask,
    @NonNull int flagsValues,
    @NonNull int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

use registerForActivityResult passing in a StartIntentSenderForResult object for the ActivityResultContract.

@NonNull void

Begin postponed transitions after postponeEnterTransition was called.

@NonNull String
@NonNull void

Prevents a context menu to be shown for the given view.

Constants

STYLE_NORMAL

@NonNull
public static final @NonNull int STYLE_NORMAL

Style for setStyle: a basic, normal dialog.

STYLE_NO_FRAME

@NonNull
public static final @NonNull int STYLE_NO_FRAME

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

STYLE_NO_INPUT

@NonNull
public static final @NonNull int STYLE_NO_INPUT

Style for setStyle: 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.

STYLE_NO_TITLE

@NonNull
public static final @NonNull int STYLE_NO_TITLE

Style for setStyle: don't include a title area.

Public constructors

DialogFragment

@NonNull
public DialogFragment()

Constructor used by the default FragmentFactory. You must set a custom FragmentFactory if you want to use a non-default constructor to ensure that your constructor is called when the fragment is re-instantiated.

It is strongly recommended to supply arguments with setArguments and later retrieved by the Fragment with getArguments. These arguments are automatically saved and restored alongside the Fragment.

Applications should generally not implement a constructor. Prefer onAttach instead. It is the first place application code can run where the fragment is ready to be used - the point where the fragment is actually associated with its context.

DialogFragment

@NonNull
public DialogFragment(@NonNull int contentLayoutId)

Alternate constructor that can be called from your default, no argument constructor to provide a default layout that will be inflated by onCreateView.

class MyDialogFragment extends DialogFragment {
  public MyDialogFragment() {
    super(R.layout.dialog_fragment_main);
  }
}
You must set a custom FragmentFactory if you want to use a non-default constructor to ensure that your constructor is called when the fragment is re-instantiated.
See also
DialogFragment

#DialogFragment()

onCreateView

#onCreateView(LayoutInflater, ViewGroup, Bundle)

Public methods

dismiss

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

dismissAllowingStateLoss

@NonNull
public void dismissAllowingStateLoss()

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

getDialog

@Nullable
@NonNull
public Dialog getDialog()

Return the Dialog this fragment is currently controlling.

See also
requireDialog

#requireDialog()

getShowsDialog

@NonNull
public boolean getShowsDialog()

Return the current value of setShowsDialog.

getTheme

@NonNull
public int getTheme()

isCancelable

@NonNull
public boolean isCancelable()

Return the current value of setCancelable.

onAttach

@NonNull
public void onAttach(@NonNull Context context)

Called when a fragment is first attached to its context. onCreate will be called after this.

onCancel

@NonNull
public void onCancel(@NonNull DialogInterface dialog)

onCreate

@NonNull
public void onCreate(@Nullable Bundle savedInstanceState)

Called to do initial creation of a fragment. This is called after onAttach and before onCreateView.

Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, add a androidx.lifecycle.LifecycleObserver on the activity's Lifecycle, removing it when it receives the Lifecycle.State#CREATED callback.

Any restored child fragments will be created before the base Fragment.onCreate method returns.

Parameters
@Nullable Bundle savedInstanceState

If the fragment is being re-created from a previous saved state, this is the state.

onCreateDialog

@NonNull
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)

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 does not need to be implemented since the AlertDialog takes care of its own content.

This method will be called after onCreate and immediately before onCreateView. 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 and onDismiss.

Parameters
@Nullable Bundle savedInstanceState

The last saved instance state of the Fragment, or null if this is a freshly created Fragment.

Returns
Dialog

Return a new Dialog instance to be displayed by the Fragment.

onDestroyView

@NonNull
public void onDestroyView()

Remove dialog.

onDetach

@NonNull
public void onDetach()

Called when the fragment is no longer attached to its activity. This is called after onDestroy.

onDismiss

@NonNull
public void onDismiss(@NonNull DialogInterface dialog)

onGetLayoutInflater

@NonNull
public LayoutInflater onGetLayoutInflater(@Nullable Bundle savedInstanceState)

Returns the LayoutInflater used to inflate Views of this Fragment. The default implementation will throw an exception if the Fragment is not attached.

If this is called from within onCreateDialog, the layout inflater from Fragment#onGetLayoutInflater(Bundle), without the dialog theme, will be returned.

onSaveInstanceState

@NonNull
public void onSaveInstanceState(@NonNull Bundle outState)

Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted. If a new instance of the fragment later needs to be created, the data you place in the Bundle here will be available in the Bundle given to onCreate, onCreateView, and onViewCreated.

This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there applies here as well. Note however: this method may be called at any time before onDestroy. There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.

Parameters
@NonNull Bundle outState

Bundle in which to place your saved state.

onStart

@NonNull
public void onStart()

Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.

onStop

@NonNull
public void onStop()

Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.

onViewStateRestored

@NonNull
public void onViewStateRestored(@Nullable Bundle savedInstanceState)

Called when all saved state has been restored into the view hierarchy of the fragment. This can be used to do initialization based on saved state that you are letting the view hierarchy track itself, such as whether check box widgets are currently checked. This is called after onViewCreated and before onStart.

Parameters
@Nullable Bundle savedInstanceState

If the fragment is being re-created from a previous saved state, this is the state.

requireDialog

@NonNull
public final Dialog requireDialog()

Return the Dialog this fragment is currently controlling.

Throws
java.lang.IllegalStateException

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

See also
getDialog

#getDialog()

setCancelable

@NonNull
public void setCancelable(@NonNull boolean cancelable)

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
@NonNull boolean cancelable

If true, the dialog is cancelable. The default is true.

setShowsDialog

@NonNull
public void setShowsDialog(@NonNull boolean showsDialog)

Controls whether this fragment should be shown in a dialog. If not set, no Dialog will be created 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.

If calling this manually, it should be called in onCreate as calling it any later will have no effect.

Parameters
@NonNull boolean showsDialog

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

@NonNull
public void setStyle(@NonNull int style, @NonNull int theme)

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
@NonNull int style

Selects a standard style: may be STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, or STYLE_NO_INPUT. Value is STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, or STYLE_NO_INPUT

@NonNull int theme

Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you.

show

@NonNull
public void show(@NonNull FragmentManager manager, @Nullable String tag)

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
@NonNull FragmentManager manager

The FragmentManager this fragment will be added to.

@Nullable String tag

The tag for this fragment, as per FragmentTransaction.add.

show

@NonNull
public int show(@NonNull FragmentTransaction transaction, @Nullable String tag)

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

Parameters
@NonNull FragmentTransaction transaction

An existing transaction in which to add the fragment.

@Nullable String tag

The tag for this fragment, as per FragmentTransaction.add.

Returns
int

Returns the identifier of the committed transaction, as per FragmentTransaction.commit().

showNow

@NonNull
public void showNow(@NonNull FragmentManager manager, @Nullable String tag)

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
@NonNull FragmentManager manager

The FragmentManager this fragment will be added to.

@Nullable String tag

The tag for this fragment, as per FragmentTransaction.add.