public abstract class PreferenceDialogFragment extends DialogFragment implements DialogInterface.OnClickListener
DialogPreference
. Since the preference object may
not be available during fragment re-creation, the necessary information for displaying the dialog
is read once during the initial call to onCreate(Bundle)
and saved/restored in the saved
instance state. Custom subclasses should also follow this pattern.Fragment.InstantiationException, Fragment.SavedState
Modifier and Type | Field and Description |
---|---|
protected static String |
ARG_KEY |
STYLE_NO_FRAME, STYLE_NO_INPUT, STYLE_NO_TITLE, STYLE_NORMAL
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
PreferenceDialogFragment() |
Modifier and Type | Method and Description |
---|---|
DialogPreference |
getPreference()
Get the preference that requested this dialog.
|
protected boolean |
needInputMethod()
Returns whether the preference needs to display a soft input method when the dialog
is displayed.
|
protected void |
onBindDialogView(View view)
Binds views in the content View of the dialog to data.
|
void |
onClick(DialogInterface dialog,
int which)
This method will be invoked when a button in the dialog is clicked.
|
void |
onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment.
|
Dialog |
onCreateDialog(Bundle savedInstanceState)
Override to build your own custom Dialog container.
|
protected View |
onCreateDialogView(Context context)
Creates the content view for the dialog (if a custom content view is
required).
|
abstract void |
onDialogClosed(boolean positiveResult) |
void |
onDismiss(DialogInterface dialog)
This method will be invoked when the dialog is dismissed.
|
protected void |
onPrepareDialogBuilder(AlertDialog.Builder builder)
Prepares the dialog builder to be shown when the preference is clicked.
|
void |
onSaveInstanceState(Bundle outState)
Called to ask the fragment to save its current dynamic state, so it
can later be reconstructed in a new instance of its process is
restarted.
|
dismiss, dismissAllowingStateLoss, dump, getDialog, getLayoutInflater, getShowsDialog, getTheme, isCancelable, onActivityCreated, onAttach, onCancel, onDestroyView, onDetach, onStart, onStop, setCancelable, setShowsDialog, setStyle, show, show, showAllowingStateLoss
equals, getActivity, getAllowEnterTransitionOverlap, getAllowReturnTransitionOverlap, getArguments, getChildFragmentManager, getContext, getEnterTransition, getExitTransition, getFragmentManager, getHost, getId, getLoaderManager, getParentFragment, getReenterTransition, getResources, getRetainInstance, getReturnTransition, getSharedElementEnterTransition, getSharedElementReturnTransition, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, hashCode, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isRemoving, isResumed, isVisible, onActivityResult, onAttach, onAttachFragment, onConfigurationChanged, onContextItemSelected, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onCreateView, onDestroy, onDestroyOptionsMenu, onHiddenChanged, onInflate, onInflate, onInflate, onLowMemory, onMultiWindowModeChanged, onOptionsItemSelected, onOptionsMenuClosed, onPause, onPictureInPictureModeChanged, onPrepareOptionsMenu, onRequestPermissionsResult, onResume, onTrimMemory, onViewCreated, onViewStateRestored, registerForContextMenu, requestPermissions, setAllowEnterTransitionOverlap, setAllowReturnTransitionOverlap, setArguments, setEnterSharedElementCallback, setEnterSharedElementTransitionCallback, setEnterTransition, setExitSharedElementCallback, setExitSharedElementTransitionCallback, setExitTransition, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setReenterTransition, setRetainInstance, setReturnTransition, setSharedElementEnterTransition, setSharedElementReturnTransition, setTargetFragment, setUserVisibleHint, shouldShowRequestPermissionRationale, startActivity, startActivity, startActivityForResult, startActivityForResult, startIntentSenderForResult, toString, unregisterForContextMenu
protected static final String ARG_KEY
public void onCreate(Bundle savedInstanceState)
Fragment
Fragment.onAttach(Activity)
and before
Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
, but is not called if the fragment
instance is retained across Activity re-creation (see Fragment.setRetainInstance(boolean)
).
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, see Fragment.onActivityCreated(Bundle)
.
If your app's targetSdkVersion
is Build.VERSION_CODES.M
or lower, child fragments being restored from the savedInstanceState are restored after
onCreate
returns. When targeting Build.VERSION_CODES.N
or
above and running on an N or newer platform version
they are restored by Fragment.onCreate
.
onCreate
in class DialogFragment
savedInstanceState
- If the fragment is being re-created from
a previous saved state, this is the state.public void onSaveInstanceState(@NonNull Bundle outState)
Fragment
Fragment.onCreate(Bundle)
,
Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
, and
Fragment.onActivityCreated(Bundle)
.
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 Fragment.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.
onSaveInstanceState
in class DialogFragment
outState
- Bundle in which to place your saved state.@NonNull public Dialog onCreateDialog(Bundle savedInstanceState)
DialogFragment
Fragment.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 DialogFragment.onCreate(Bundle)
and
before Fragment.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 DialogFragment.onCancel(DialogInterface)
and DialogFragment.onDismiss(DialogInterface)
.
onCreateDialog
in class DialogFragment
savedInstanceState
- The last saved instance state of the Fragment,
or null if this is a freshly created Fragment.public DialogPreference getPreference()
onCreate(Bundle)
has
been called on the PreferenceFragment
which launched this dialog.DialogPreference
associated with this
dialog.protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
Do not AlertDialog.Builder#create()
or
AlertDialog.Builder#show()
.
protected boolean needInputMethod()
protected View onCreateDialogView(Context context)
Preference.setLayoutResource(int)
protected void onBindDialogView(View view)
Make sure to call through to the superclass implementation.
view
- The content View of the dialog, if it is custom.public void onClick(DialogInterface dialog, int which)
DialogInterface.OnClickListener
onClick
in interface DialogInterface.OnClickListener
dialog
- The dialog that received the click.which
- The button that was clicked (e.g.
DialogInterface.BUTTON1
) or the position
of the item clicked.public void onDismiss(DialogInterface dialog)
DialogInterface.OnDismissListener
onDismiss
in interface DialogInterface.OnDismissListener
onDismiss
in class DialogFragment
dialog
- The dialog that was dismissed will be passed into the
method.public abstract void onDialogClosed(boolean positiveResult)