Android Studio

AppCompatDialogFragment


public class AppCompatDialogFragment extends DialogFragment
java.lang.Object
   ↳ androidx.fragment.app.Fragment
     ↳ androidx.fragment.app.DialogFragment
       ↳ androidx.appcompat.app.AppCompatDialogFragment

A special version of DialogFragment which uses an AppCompatDialog in place of a platform-styled dialog.

See also
DialogFragment

Summary

Public constructors

AppCompatDialogFragment(@LayoutRes int contentLayoutId)

Public methods

@NonNull Dialog
onCreateDialog(@Nullable Bundle savedInstanceState)

Override to build your own custom Dialog container.

Inherited Constants

From androidx.fragment.app.DialogFragment
static final int

Style for setStyle: a basic, normal dialog.

static final 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 int

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

static final int

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

Inherited methods

From androidx.activity.result.ActivityResultCaller
abstract ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    ActivityResultContract<I, O> contract,
    ActivityResultCallback<O> callback
)

Register a request to start an activity for result, designated by the given contract.

From android.content.ComponentCallbacks
abstract void
abstract void

This method is deprecated.

From androidx.fragment.app.DialogFragment
void

Dismiss the fragment and its dialog.

void

Version of dismiss that uses FragmentTransaction.commitAllowingStateLoss().

void

Version of dismiss that uses commitNow.

@Nullable Dialog

Return the Dialog this fragment is currently controlling.

boolean

Return the current value of setShowsDialog.

@StyleRes int
boolean

Return the current value of setCancelable.

void

This method is deprecated.

use onCreateDialog for code touching the dialog created by onCreateDialog, onViewCreated for code touching the view created by onCreateView and onCreate for other initialization.

void

Called when a fragment is first attached to its context.

void
void
@MainThread
onCreate(@Nullable Bundle savedInstanceState)

Called to do initial creation of a fragment.

void

Remove dialog.

void

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

void
@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)

Returns the LayoutInflater used to inflate Views of this Fragment.

void

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.

void

Called when the Fragment is visible to the user.

void

Called when the Fragment is no longer started.

void

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

final @NonNull ComponentDialog

Return the ComponentDialog this fragment is currently controlling.

final @NonNull Dialog

Return the Dialog this fragment is currently controlling.

void
setCancelable(boolean cancelable)

Control whether the shown Dialog is cancelable.

void
setShowsDialog(boolean showsDialog)

Controls whether this fragment should be shown in a dialog.

void
setStyle(int style, @StyleRes int theme)

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

void

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

int

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

void

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

From android.content.DialogInterface.OnCancelListener
abstract void
From android.content.DialogInterface.OnDismissListener
abstract void
From androidx.fragment.app.Fragment void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable String[] args
)

Print the Fragments's state into the given stream.

final boolean
equals(@Nullable Object o)

Subclasses can not override equals().

final @Nullable FragmentActivity
getActivity()

Return the FragmentActivity this fragment is currently associated with.

boolean
getAllowEnterTransitionOverlap()

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

boolean
getAllowReturnTransitionOverlap()

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

final @Nullable Bundle
getArguments()

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

final @NonNull FragmentManager
getChildFragmentManager()

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

@Nullable Context
getContext()

Return the Context this fragment is currently associated with.

@NonNull CreationExtras
@CallSuper
getDefaultViewModelCreationExtras()

Returns the default CreationExtras that should be passed into ViewModelProvider.Factory.create when no overriding CreationExtras were passed to the ViewModelProvider constructors.

@NonNull ViewModelProvider.Factory
getDefaultViewModelProviderFactory()

Returns the default ViewModelProvider.Factory that should be used when no custom Factory is provided to the ViewModelProvider constructors.

@Nullable Object
getEnterTransition()

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

@Nullable Object
getExitTransition()

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
getFragmentManager()

This method is deprecated.

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

final @Nullable Object
getHost()

Return the host object of this fragment.

final int
getId()

Return the identifier this fragment is known by.

final @NonNull LayoutInflater
getLayoutInflater()

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

@NonNull Lifecycle
getLifecycle()

Returns the Lifecycle of the provider.

@NonNull LoaderManager
getLoaderManager()

This method is deprecated.

Use LoaderManager.getInstance(this).

final @Nullable Fragment
getParentFragment()

Returns the parent Fragment containing this Fragment.

final @NonNull FragmentManager
getParentFragmentManager()

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

@Nullable Object
getReenterTransition()

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
getResources()

Return requireActivity().getResources().

final boolean
getRetainInstance()

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
getReturnTransition()

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
getSavedStateRegistry()

The SavedStateRegistry owned by this SavedStateRegistryOwner

@Nullable Object
getSharedElementEnterTransition()

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

@Nullable Object
getSharedElementReturnTransition()

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

final @NonNull String
getString(@StringRes int resId)

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

final @NonNull String
getString(@StringRes int resId, @Nullable 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
getTag()

Get the tag name of the fragment, if specified.

final @Nullable Fragment
getTargetFragment()

This method is deprecated.

Instead of using a target fragment to pass results, use setFragmentResult to deliver results to FragmentResultListener instances registered by other fragments via setFragmentResultListener.

final int
getTargetRequestCode()

This method is deprecated.

When using the target fragment replacement of setFragmentResultListener and setFragmentResult, consider using setArguments to pass a requestKey if you need to support dynamic request keys.

final @NonNull CharSequence
getText(@StringRes int resId)

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

boolean
getUserVisibleHint()

This method is deprecated.

Use setMaxLifecycle instead.

@Nullable View
getView()

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

@NonNull LifecycleOwner
@MainThread
getViewLifecycleOwner()

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

@NonNull LiveData<LifecycleOwner>
getViewLifecycleOwnerLiveData()

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

@NonNull ViewModelStore
getViewModelStore()

Returns the ViewModelStore associated with this Fragment

final int
hashCode()

Subclasses can not override hashCode().

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

This method is deprecated.

Use getFragmentFactory and instantiate

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

This method is deprecated.

Use getFragmentFactory and instantiate, manually calling setArguments on the returned Fragment.

final boolean
isAdded()

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

final boolean
isDetached()

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

final boolean
isHidden()

Return true if the fragment has been hidden.

final boolean
isInLayout()
Return true if the layout is included as part of an activity view hierarchy via the tag. final boolean
isRemoving()

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

final boolean
isResumed()

Return true if the fragment is in the resumed state.

final boolean
isStateSaved()

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

final boolean
isVisible()

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

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

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
@MainThread
@CallSuper
onAttach(@NonNull Activity activity)

This method is deprecated.

See onAttach.

void
@MainThread
onAttachFragment(@NonNull Fragment childFragment)

This method is deprecated.

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

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

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

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

Called when a fragment loads an animation.

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

Called when a fragment loads an animator.

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

Called when a context menu for the view is about to be shown.

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

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

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

Called to have the fragment instantiate its user interface view.

void
@MainThread
@CallSuper
onDestroy()

Called when the fragment is no longer in use.

void
@MainThread
onDestroyOptionsMenu()

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
onHiddenChanged(boolean hidden)

Called when the hidden state (as returned by isHidden of the fragment or another fragment in its hierarchy has changed.

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

This method is deprecated.

See onInflate.

void
@UiThread
@CallSuper
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.

void
@MainThread
@CallSuper
onLowMemory()

This method is deprecated.

void
onMultiWindowModeChanged(boolean isInMultiWindowMode)

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

boolean
@MainThread
onOptionsItemSelected(@NonNull MenuItem item)

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
onOptionsMenuClosed(@NonNull Menu menu)

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
@CallSuper
onPause()

Called when the Fragment is no longer resumed.

void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

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

void
@MainThread
onPrepareOptionsMenu(@NonNull Menu menu)

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)

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

void
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
@MainThread
@CallSuper
onResume()

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

void
@MainThread
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.

void
postponeEnterTransition()

Postpone the entering Fragment transition until startPostponedEnterTransition or executePendingTransactions has been called.

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

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

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

Register a request to start an activity for result, designated by the given contract.

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

Register a request to start an activity for result, designated by the given contract.

void
registerForContextMenu(@NonNull View view)

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

final void
requestPermissions(@NonNull String[] permissions, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

final @NonNull FragmentActivity
requireActivity()

Return the FragmentActivity this fragment is currently associated with.

final @NonNull Bundle
requireArguments()

Return the arguments supplied when the fragment was instantiated.

final @NonNull Context
requireContext()

Return the Context this fragment is currently associated with.

final @NonNull FragmentManager
requireFragmentManager()

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
requireHost()

Return the host object of this fragment.

final @NonNull Fragment
requireParentFragment()

Returns the parent Fragment containing this Fragment.

final @NonNull View
requireView()

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

void
setAllowEnterTransitionOverlap(boolean allow)

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

void
setAllowReturnTransitionOverlap(boolean allow)

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

void
setArguments(@Nullable Bundle args)

Supply the construction arguments for this fragment.

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.

void
setEnterTransition(@Nullable Object transition)

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

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.

void
setExitTransition(@Nullable Object transition)

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.

void
setHasOptionsMenu(boolean hasMenu)

This method is deprecated.

This method is no longer needed when using a MenuProvider to provide a Menu to your activity, which replaces onCreateOptionsMenu as the recommended way to provide a consistent, optionally Lifecycle-aware, and modular way to handle menu creation and item selection.

void
setInitialSavedState(@Nullable Fragment.SavedState state)

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

void
setMenuVisibility(boolean menuVisible)

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

void
setReenterTransition(@Nullable Object transition)

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

void
setRetainInstance(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.

void
setReturnTransition(@Nullable Object transition)

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.

void
setSharedElementEnterTransition(@Nullable Object transition)

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

void
setSharedElementReturnTransition(@Nullable Object transition)

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

void
setTargetFragment(@Nullable Fragment fragment, int requestCode)

This method is deprecated.

Instead of using a target fragment to pass results, the fragment requesting a result should use setFragmentResultListener to register a FragmentResultListener with a requestKey using its parent fragment manager.

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated.

If you are manually calling this method, use setMaxLifecycle instead.

boolean
shouldShowRequestPermissionRationale(@NonNull String permission)

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

void
startActivity(@NonNull Intent intent)

Call startActivity from the fragment's containing Activity.

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

Call startActivity from the fragment's containing Activity.

void
startActivityForResult(@NonNull Intent intent, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

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

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

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

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
startPostponedEnterTransition()

Begin postponed transitions after postponeEnterTransition was called.

@NonNull String
toString()
void
unregisterForContextMenu(@NonNull View view)

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

From androidx.lifecycle.HasDefaultViewModelProviderFactory
CreationExtras

Returns the default CreationExtras that should be passed into ViewModelProvider.Factory.create when no overriding CreationExtras were passed to the ViewModelProvider constructors.

ViewModelProvider.Factory

Returns the default ViewModelProvider.Factory that should be used when no custom Factory is provided to the ViewModelProvider constructors.

From androidx.lifecycle.LifecycleOwner
abstract Lifecycle

Returns the Lifecycle of the provider.

From androidx.savedstate.SavedStateRegistryOwner
abstract SavedStateRegistry

The SavedStateRegistry owned by this SavedStateRegistryOwner

From android.view.View.OnCreateContextMenuListener
abstract void
From androidx.lifecycle.ViewModelStoreOwner
abstract ViewModelStore

The owned ViewModelStore

Public constructors

AppCompatDialogFragment

Added in 1.1.0
public AppCompatDialogFragment()

AppCompatDialogFragment

Added in 1.4.0
public AppCompatDialogFragment(@LayoutRes int contentLayoutId)

Public methods

onCreateDialog

public @NonNull 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
@NonNull Dialog

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