{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
public class DialogFragment extends Fragment
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.
Constants |
|
---|---|
static final @NonNull int |
Style for |
static final @NonNull int |
Style for |
static final @NonNull int |
Style for |
static final @NonNull int |
Style for |
Public constructors |
|
---|---|
Constructor used by the default |
|
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 |
Public methods |
|
---|---|
@NonNull void |
dismiss() Dismiss the fragment and its dialog. |
@NonNull void |
Version of |
@Nullable Dialog |
Return the Dialog this fragment is currently controlling. |
@NonNull boolean |
Return the current value of |
@NonNull int |
getTheme() |
@NonNull boolean |
Return the current value of |
@NonNull void |
Called when a fragment is first attached to its context. |
@NonNull void |
|
@NonNull void |
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 |
onDetach() Called when the fragment is no longer attached to its activity. |
@NonNull void |
|
@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 |
onStart() Called when the Fragment is visible to the user. |
@NonNull void |
onStop() 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 |
Call to customize the basic appearance and behavior of the fragment's dialog. |
@NonNull void |
show(@NonNull FragmentManager manager, @Nullable String tag) Display the dialog, adding the fragment to the given FragmentManager. |
@NonNull int |
show(@NonNull FragmentTransaction transaction, @Nullable String tag) Display the dialog, adding the fragment using an existing transaction and then |
@NonNull void |
showNow(@NonNull FragmentManager manager, @Nullable String tag) Display the dialog, immediately adding the fragment to the given FragmentManager. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@NonNull
public static final @NonNull int STYLE_NORMAL
Style for setStyle
: a basic, normal dialog.
@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.
@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.
@NonNull
public static final @NonNull int STYLE_NO_TITLE
Style for setStyle
: don't include a title area.
@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.
@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) |
@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.
@NonNull
public void dismissAllowingStateLoss()
Version of dismiss
that uses FragmentTransaction.commitAllowingStateLoss()
. See linked documentation for further details.
@Nullable
@NonNull
public Dialog getDialog()
Return the Dialog this fragment is currently controlling.
See also | |
---|---|
requireDialog |
#requireDialog() |
@NonNull
public void onAttach(@NonNull Context context)
Called when a fragment is first attached to its context. onCreate
will be called after this.
@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. |
@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. |
@NonNull
public void onDetach()
Called when the fragment is no longer attached to its activity. This is called after onDestroy
.
@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.
@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. |
@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.
@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.
@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. |
@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 |
See also | |
---|---|
getDialog |
#getDialog() |
@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. |
@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. |
@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 |
@NonNull int theme |
Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you. |
@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 |
@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 |
Returns | |
---|---|
int |
Returns the identifier of the committed transaction, as per |
@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 |