PreferenceFragment
abstract classPreferenceFragment: Fragment, PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment
kotlin.Any | ||
↳ | android.app.Fragment | |
↳ | androidx.preference.PreferenceFragment |
Shows a hierarchy of Preference
objects as lists. These preferences will automatically save to android.content.SharedPreferences
as the user interacts with them. To retrieve an instance of android.content.SharedPreferences
that the preference hierarchy in this fragment will use, call PreferenceManager#getDefaultSharedPreferences(android.content.Context)
with a context in the same package as this fragment.
Furthermore, the preferences shown will follow the visual style of system preferences. It is easy to create a hierarchy of preferences (that can be shown on multiple screens) via XML. For these reasons, it is recommended to use this fragment (as a superclass) to deal with preferences in applications.
A PreferenceScreen
object should be at the top of the preference hierarchy. Furthermore, subsequent PreferenceScreen
in the hierarchy denote a screen break--that is the preferences contained within subsequent PreferenceScreen
should be shown on another screen. The preference framework handles this by calling onNavigateToScreen(PreferenceScreen)
.
The preference hierarchy can be formed in multiple ways:
Activities
that each specify its own preferences in an XML file via android.app.Activity
meta-data PreferenceScreen
To inflate from XML, use the addPreferencesFromResource(int)
. The root element should be a PreferenceScreen
. Subsequent elements can point to actual Preference
subclasses. As mentioned above, subsequent PreferenceScreen
in the hierarchy will result in the screen break.
To specify an object hierarchy rooted with PreferenceScreen
, use setPreferenceScreen(PreferenceScreen)
.
As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick(Preference)
.
Summary
Nested classes |
|
---|---|
abstract |
Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. |
abstract |
Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. |
abstract |
Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a new screen of preferences. |
Constants |
|
---|---|
static String |
Fragment argument used to specify the tag of the desired root |
Public constructors |
|
---|---|
<init>() Shows a hierarchy of |
Public methods |
|
---|---|
open Unit |
addPreferencesFromResource(@XmlRes preferencesResId: Int) Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy. |
open T? |
findPreference(key: CharSequence) Finds a |
RecyclerView! | |
open PreferenceManager! |
Returns the |
open PreferenceScreen! |
Gets the root of the preference hierarchy that this fragment is showing. |
open Unit | |
open RecyclerView.LayoutManager! |
Called from |
abstract Unit |
onCreatePreferences(savedInstanceState: Bundle!, rootKey: String!) Called during |
open RecyclerView! |
onCreateRecyclerView(inflater: LayoutInflater!, parent: ViewGroup!, savedInstanceState: Bundle!) Creates the |
open View! |
onCreateView(inflater: LayoutInflater!, container: ViewGroup!, savedInstanceState: Bundle!) |
open Unit | |
open Unit |
onDisplayPreferenceDialog(preference: Preference!) Called when a preference in the tree requests to display a dialog. |
open Unit |
Called by |
open Boolean |
onPreferenceTreeClick(preference: Preference!) Called when a preference in the tree rooted at this |
open Unit |
onSaveInstanceState(outState: Bundle!) |
open Unit |
onStart() |
open Unit |
onStop() |
open Unit |
onViewCreated(view: View!, savedInstanceState: Bundle!) |
open Unit |
scrollToPreference(key: String!) |
open Unit |
scrollToPreference(preference: Preference!) |
open Unit |
setDivider(divider: Drawable!) Sets the |
open Unit |
setDividerHeight(height: Int) Sets the height of the divider that will be drawn between each item in the list. |
open Unit |
setPreferenceScreen(preferenceScreen: PreferenceScreen!) Sets the root of the preference hierarchy that this fragment is showing. |
open Unit |
setPreferencesFromResource(@XmlRes preferencesResId: Int, @Nullable key: String?) Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at |
Protected methods |
|
---|---|
open RecyclerView.Adapter<RecyclerView.ViewHolder!>! |
onCreateAdapter(preferenceScreen: PreferenceScreen!) Creates the root adapter. |
Constants
ARG_PREFERENCE_ROOT
static valARG_PREFERENCE_ROOT: String
Deprecated: Use PreferenceFragmentCompat
instead
Fragment argument used to specify the tag of the desired root PreferenceScreen
object.
Value: "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"
Public constructors
<init>
PreferenceFragment()
Deprecated: Use PreferenceFragmentCompat
instead
Shows a hierarchy of Preference
objects as lists. These preferences will automatically save to android.content.SharedPreferences
as the user interacts with them. To retrieve an instance of android.content.SharedPreferences
that the preference hierarchy in this fragment will use, call PreferenceManager#getDefaultSharedPreferences(android.content.Context)
with a context in the same package as this fragment.
Furthermore, the preferences shown will follow the visual style of system preferences. It is easy to create a hierarchy of preferences (that can be shown on multiple screens) via XML. For these reasons, it is recommended to use this fragment (as a superclass) to deal with preferences in applications.
A PreferenceScreen
object should be at the top of the preference hierarchy. Furthermore, subsequent PreferenceScreen
in the hierarchy denote a screen break--that is the preferences contained within subsequent PreferenceScreen
should be shown on another screen. The preference framework handles this by calling onNavigateToScreen(PreferenceScreen)
.
The preference hierarchy can be formed in multiple ways:
Activities
that each specify its own preferences in an XML file via android.app.Activity
meta-data PreferenceScreen
To inflate from XML, use the addPreferencesFromResource(int)
. The root element should be a PreferenceScreen
. Subsequent elements can point to actual Preference
subclasses. As mentioned above, subsequent PreferenceScreen
in the hierarchy will result in the screen break.
To specify an object hierarchy rooted with PreferenceScreen
, use setPreferenceScreen(PreferenceScreen)
.
As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick(Preference)
.
See Also
Public methods
addPreferencesFromResource
open funaddPreferencesFromResource(@XmlRes preferencesResId: Int): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
Parameters | |
---|---|
preferencesResId |
Int: The XML resource ID to inflate |
findPreference
open fun <T : Preference!>findPreference(key: CharSequence): T?
Deprecated: Use PreferenceFragmentCompat
instead
Finds a Preference
based on its key.
Parameters | |
---|---|
key |
CharSequence: The key of the preference to retrieve |
Return | |
---|---|
T?: The Preference with the key, or null |
getPreferenceManager
open fungetPreferenceManager(): PreferenceManager!
Deprecated: Use PreferenceFragmentCompat
instead
Returns the PreferenceManager
used by this fragment.
Return | |
---|---|
PreferenceManager!: The PreferenceManager used by this fragment |
getPreferenceScreen
open fungetPreferenceScreen(): PreferenceScreen!
Deprecated: Use PreferenceFragmentCompat
instead
Gets the root of the preference hierarchy that this fragment is showing.
Return | |
---|---|
PreferenceScreen!: The PreferenceScreen that is the root of the preference hierarchy |
onCreateLayoutManager
open funonCreateLayoutManager(): RecyclerView.LayoutManager!
Deprecated: Use PreferenceFragmentCompat
instead
Called from onCreateRecyclerView
to create the RecyclerView.LayoutManager
for the created RecyclerView
.
Return | |
---|---|
RecyclerView.LayoutManager!: A new RecyclerView.LayoutManager instance |
onCreatePreferences
abstract funonCreatePreferences(savedInstanceState: Bundle!, rootKey: String!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Called during onCreate(Bundle)
to supply the preferences for this fragment. Subclasses are expected to call setPreferenceScreen(PreferenceScreen)
either directly or via helper methods such as addPreferencesFromResource(int)
.
Parameters | |
---|---|
savedInstanceState |
Bundle!: If the fragment is being re-created from a previous saved state, this is the state. |
rootKey |
Bundle!: If non-null, this preference fragment should be rooted at the PreferenceScreen with this key. |
onCreateRecyclerView
open funonCreateRecyclerView(inflater: LayoutInflater!, parent: ViewGroup!, savedInstanceState: Bundle!): RecyclerView!
Deprecated: Use PreferenceFragmentCompat
instead
Creates the RecyclerView
used to display the preferences. Subclasses may override this to return a customized RecyclerView
.
Parameters | |
---|---|
inflater |
LayoutInflater!: The LayoutInflater object that can be used to inflate the RecyclerView . |
parent |
LayoutInflater!: The parent view that the RecyclerView will be attached to. This method should not add the view itself, but this can be used to generate the layout params of the view. |
savedInstanceState |
LayoutInflater!: If non-null, this view is being re-constructed from a previous saved state as given here. |
Return | |
---|---|
RecyclerView!: A new RecyclerView object to be placed into the view hierarchy |
onCreateView
open fun onCreateView(inflater: LayoutInflater!, container: ViewGroup!, savedInstanceState: Bundle!): View!
onDestroyView
open fun onDestroyView(): Unit
onDisplayPreferenceDialog
open funonDisplayPreferenceDialog(preference: Preference!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Called when a preference in the tree requests to display a dialog. Subclasses should override this method to display custom dialogs or to handle dialogs for custom preference classes.
Parameters | |
---|---|
preference |
Preference!: The Preference object requesting the dialog |
onNavigateToScreen
open funonNavigateToScreen(: PreferenceScreen!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Called by PreferenceScreen#onClick()
in order to navigate to a new screen of preferences. Calls PreferenceFragment.OnPreferenceStartScreenCallback#onPreferenceStartScreen
if the target fragment or containing activity implements PreferenceFragment.OnPreferenceStartScreenCallback
.
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen!: The PreferenceScreen to navigate to |
onPreferenceTreeClick
open funonPreferenceTreeClick(preference: Preference!): Boolean
Deprecated: Use PreferenceFragmentCompat
instead
Called when a preference in the tree rooted at this PreferenceScreen
has been clicked.
Parameters | |
---|---|
preference |
Preference!: The preference that was clicked |
Return | |
---|---|
Boolean: Whether the click was handled |
onStart
open fun onStart(): Unit
onStop
open fun onStop(): Unit
scrollToPreference
open funscrollToPreference(key: String!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
scrollToPreference
open funscrollToPreference(preference: Preference!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
setDivider
open funsetDivider(divider: Drawable!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Sets the Drawable
that will be drawn between each item in the list.
Note: If the drawable does not have an intrinsic height, you should also call setDividerHeight(int)
.
Parameters | |
---|---|
divider |
Drawable!: The drawable to use android.R.attr#divider |
setDividerHeight
open funsetDividerHeight(height: Int): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Sets the height of the divider that will be drawn between each item in the list. Calling this will override the intrinsic height as set by setDivider(Drawable)
.
Parameters | |
---|---|
height |
Int: The new height of the divider in pixels android.R.attr#dividerHeight |
setPreferenceScreen
open funsetPreferenceScreen(preferenceScreen: PreferenceScreen!): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Sets the root of the preference hierarchy that this fragment is showing.
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen!: The root PreferenceScreen of the preference hierarchy |
setPreferencesFromResource
open funsetPreferencesFromResource(@XmlRes preferencesResId: Int, @Nullable key: String?): Unit
Deprecated: Use PreferenceFragmentCompat
instead
Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key
.
Parameters | |
---|---|
preferencesResId |
Int: The XML resource ID to inflate |
key |
Int: The preference key of the PreferenceScreen to use as the root of the preference hierarchy, or null to use the root PreferenceScreen . |
Protected methods
onCreateAdapter
protected open funonCreateAdapter(preferenceScreen: PreferenceScreen!): RecyclerView.Adapter<RecyclerView.ViewHolder!>!
Deprecated: Use PreferenceFragmentCompat
instead
Creates the root adapter.
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen!: The PreferenceScreen object to create the adapter for |
Return | |
---|---|
RecyclerView.Adapter<RecyclerView.ViewHolder!>!: An adapter that contains the preferences contained in this PreferenceScreen |