belongs to Maven artifact com.android.support:preference-v7:28.0.0-alpha1
PreferenceManager
public
class
PreferenceManager
extends Object
java.lang.Object | |
↳ | android.support.v7.preference.PreferenceManager |
Used to help create Preference
hierarchies
from activities or XML.
In most cases, clients should use
addPreferencesFromResource(int)
, or
addPreferencesFromResource(int)
.
Summary
Nested classes | |
---|---|
interface |
PreferenceManager.OnDisplayPreferenceDialogListener
Interface definition for a class that will be called when a
|
interface |
PreferenceManager.OnNavigateToScreenListener
Interface definition for a class that will be called when a
|
interface |
PreferenceManager.OnPreferenceTreeClickListener
Interface definition for a callback to be invoked when a
|
class |
PreferenceManager.PreferenceComparisonCallback
Callback class to be used by the |
class |
PreferenceManager.SimplePreferenceComparisonCallback
A basic implementation of |
Constants | |
---|---|
String |
KEY_HAS_SET_DEFAULT_VALUES
|
Public methods | |
---|---|
PreferenceScreen
|
createPreferenceScreen(Context context)
|
Preference
|
findPreference(CharSequence key)
Finds a |
Context
|
getContext()
Returns the context. |
static
SharedPreferences
|
getDefaultSharedPreferences(Context context)
Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context. |
PreferenceManager.OnDisplayPreferenceDialogListener
|
getOnDisplayPreferenceDialogListener()
|
PreferenceManager.OnNavigateToScreenListener
|
getOnNavigateToScreenListener()
Returns the |
PreferenceManager.OnPreferenceTreeClickListener
|
getOnPreferenceTreeClickListener()
|
PreferenceManager.PreferenceComparisonCallback
|
getPreferenceComparisonCallback()
|
PreferenceDataStore
|
getPreferenceDataStore()
Returns the |
PreferenceScreen
|
getPreferenceScreen()
Returns the root of the preference hierarchy managed by this class. |
SharedPreferences
|
getSharedPreferences()
Gets a |
int
|
getSharedPreferencesMode()
Returns the current mode of the SharedPreferences file that preferences managed by this will use. |
String
|
getSharedPreferencesName()
Returns the current name of the |
boolean
|
isStorageDefault()
Indicates if the storage location used internally by this class is the
default provided by the hosting |
boolean
|
isStorageDeviceProtected()
Indicates if the storage location used internally by this class is backed by device-protected storage. |
static
void
|
setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
Similar to |
static
void
|
setDefaultValues(Context context, int resId, boolean readAgain)
Sets the default values from an XML preference file by reading the values defined
by each |
void
|
setOnDisplayPreferenceDialogListener(PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener)
|
void
|
setOnNavigateToScreenListener(PreferenceManager.OnNavigateToScreenListener listener)
Sets the callback to be invoked when a |
void
|
setOnPreferenceTreeClickListener(PreferenceManager.OnPreferenceTreeClickListener listener)
Sets the callback to be invoked when a |
void
|
setPreferenceComparisonCallback(PreferenceManager.PreferenceComparisonCallback preferenceComparisonCallback)
|
void
|
setPreferenceDataStore(PreferenceDataStore dataStore)
Sets a |
boolean
|
setPreferences(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy. |
void
|
setSharedPreferencesMode(int sharedPreferencesMode)
Sets the mode of the SharedPreferences file that preferences managed by this will use. |
void
|
setSharedPreferencesName(String sharedPreferencesName)
Sets the name of the |
void
|
setStorageDefault()
Sets the storage location used internally by this class to be the default
provided by the hosting |
void
|
setStorageDeviceProtected()
Explicitly set the storage location used internally by this class to be device-protected storage. |
void
|
showDialog(Preference preference)
Called when a preference requests that a dialog be shown to complete a user interaction. |
Inherited methods | |
---|---|
From
class
java.lang.Object
|
Constants
KEY_HAS_SET_DEFAULT_VALUES
String KEY_HAS_SET_DEFAULT_VALUES
Constant Value: "_has_set_default_values"
Public methods
createPreferenceScreen
PreferenceScreen createPreferenceScreen (Context context)
Parameters | |
---|---|
context |
Context |
Returns | |
---|---|
PreferenceScreen |
findPreference
Preference findPreference (CharSequence key)
Finds a Preference
based on its key.
Parameters | |
---|---|
key |
CharSequence : The key of the preference to retrieve. |
Returns | |
---|---|
Preference |
The Preference with the key, or null. |
See also:
getContext
Context getContext ()
Returns the context.
Returns | |
---|---|
Context |
The context. |
getDefaultSharedPreferences
SharedPreferences getDefaultSharedPreferences (Context context)
Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
Parameters | |
---|---|
context |
Context : The context of the preferences whose values are wanted. |
Returns | |
---|---|
SharedPreferences |
A SharedPreferences instance that can be used to retrieve and listen to values of the preferences. |
getOnDisplayPreferenceDialogListener
PreferenceManager.OnDisplayPreferenceDialogListener getOnDisplayPreferenceDialogListener ()
Returns | |
---|---|
PreferenceManager.OnDisplayPreferenceDialogListener |
getOnNavigateToScreenListener
PreferenceManager.OnNavigateToScreenListener getOnNavigateToScreenListener ()
Returns the PreferenceManager.OnNavigateToScreenListener
, if one has been set.
Returns | |
---|---|
PreferenceManager.OnNavigateToScreenListener |
getOnPreferenceTreeClickListener
PreferenceManager.OnPreferenceTreeClickListener getOnPreferenceTreeClickListener ()
Returns | |
---|---|
PreferenceManager.OnPreferenceTreeClickListener |
getPreferenceComparisonCallback
PreferenceManager.PreferenceComparisonCallback getPreferenceComparisonCallback ()
Returns | |
---|---|
PreferenceManager.PreferenceComparisonCallback |
getPreferenceDataStore
PreferenceDataStore getPreferenceDataStore ()
Returns the PreferenceDataStore
associated with this manager or null
if
the default SharedPreferences
are used instead.
Returns | |
---|---|
PreferenceDataStore |
The PreferenceDataStore associated with this manager or null if none. |
getPreferenceScreen
PreferenceScreen getPreferenceScreen ()
Returns the root of the preference hierarchy managed by this class.
Returns | |
---|---|
PreferenceScreen |
The PreferenceScreen object that is at the root of the hierarchy.
|
getSharedPreferences
SharedPreferences getSharedPreferences ()
Gets a SharedPreferences
instance that preferences managed by this will
use.
Returns | |
---|---|
SharedPreferences |
a SharedPreferences instance pointing to the file that contain the values of
preferences that are managed by this PreferenceManager. If
a PreferenceDataStore has been set, this method returns null .
|
getSharedPreferencesMode
int getSharedPreferencesMode ()
Returns the current mode of the SharedPreferences file that preferences managed by this will use.
Returns | |
---|---|
int |
The mode that can be passed to getSharedPreferences(String, int) . |
See also:
getSharedPreferencesName
String getSharedPreferencesName ()
Returns the current name of the SharedPreferences
file that preferences managed by
this will use.
Returns | |
---|---|
String |
The name that can be passed to getSharedPreferences(String, int) . |
See also:
isStorageDefault
boolean isStorageDefault ()
Indicates if the storage location used internally by this class is the
default provided by the hosting Context
.
Returns | |
---|---|
boolean |
isStorageDeviceProtected
boolean isStorageDeviceProtected ()
Indicates if the storage location used internally by this class is backed by device-protected storage.
Returns | |
---|---|
boolean |
setDefaultValues
void setDefaultValues (Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
Similar to setDefaultValues(Context, int, boolean)
but allows
the client to provide the filename and mode of the shared preferences
file.
Parameters | |
---|---|
context |
Context : The context of the shared preferences. |
sharedPreferencesName |
String : A custom name for the shared preferences file. |
sharedPreferencesMode |
int : The file creation mode for the shared preferences file, such
as MODE_PRIVATE or MODE_PRIVATE |
resId |
int : The resource ID of the preference XML file. |
readAgain |
boolean : Whether to re-read the default values.
If false, this method will set the default values only if this
method has never been called in the past (or if the
KEY_HAS_SET_DEFAULT_VALUES in the default value shared
preferences file is false). To attempt to set the default values again
bypassing this check, set readAgain to true.
Note: this will NOT reset preferences back to their default
values. For that functionality, use
|
setDefaultValues
void setDefaultValues (Context context, int resId, boolean readAgain)
Sets the default values from an XML preference file by reading the values defined
by each Preference
item's android:defaultValue
attribute. This should
be called by the application's main activity.
Parameters | |
---|---|
context |
Context : The context of the shared preferences. |
resId |
int : The resource ID of the preference XML file. |
readAgain |
boolean : Whether to re-read the default values.
If false, this method sets the default values only if this
method has never been called in the past (or if the
KEY_HAS_SET_DEFAULT_VALUES in the default value shared
preferences file is false). To attempt to set the default values again
bypassing this check, set readAgain to true.
Note: this will NOT reset preferences back to their default
values. For that functionality, use
|
setOnDisplayPreferenceDialogListener
void setOnDisplayPreferenceDialogListener (PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener)
Parameters | |
---|---|
onDisplayPreferenceDialogListener |
PreferenceManager.OnDisplayPreferenceDialogListener |
setOnNavigateToScreenListener
void setOnNavigateToScreenListener (PreferenceManager.OnNavigateToScreenListener listener)
Sets the callback to be invoked when a PreferenceScreen
in the hierarchy rooted at
this PreferenceManager
is clicked.
Parameters | |
---|---|
listener |
PreferenceManager.OnNavigateToScreenListener : The callback to be invoked.
|
setOnPreferenceTreeClickListener
void setOnPreferenceTreeClickListener (PreferenceManager.OnPreferenceTreeClickListener listener)
Sets the callback to be invoked when a Preference
in the
hierarchy rooted at this PreferenceManager
is clicked.
Parameters | |
---|---|
listener |
PreferenceManager.OnPreferenceTreeClickListener : The callback to be invoked.
|
setPreferenceComparisonCallback
void setPreferenceComparisonCallback (PreferenceManager.PreferenceComparisonCallback preferenceComparisonCallback)
Parameters | |
---|---|
preferenceComparisonCallback |
PreferenceManager.PreferenceComparisonCallback |
setPreferenceDataStore
void setPreferenceDataStore (PreferenceDataStore dataStore)
Sets a PreferenceDataStore
to be used by all Preferences associated with this manager
that don't have a custom PreferenceDataStore
assigned via
setPreferenceDataStore(PreferenceDataStore)
. Also if the data store is
set, the child preferences won't use SharedPreferences
as long as
they are assigned to this manager.
Parameters | |
---|---|
dataStore |
PreferenceDataStore : the PreferenceDataStore to be used by this manager |
setPreferences
boolean setPreferences (PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy.
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen : The root PreferenceScreen of the preference hierarchy. |
Returns | |
---|---|
boolean |
Whether the PreferenceScreen given is different than the previous.
|
setSharedPreferencesMode
void setSharedPreferencesMode (int sharedPreferencesMode)
Sets the mode of the SharedPreferences file that preferences managed by this will use.
Parameters | |
---|---|
sharedPreferencesMode |
int : The mode of the SharedPreferences file. |
See also:
setSharedPreferencesName
void setSharedPreferencesName (String sharedPreferencesName)
Sets the name of the SharedPreferences
file that preferences managed by this
will use.
If custom PreferenceDataStore
is set, this won't override its usage.
Parameters | |
---|---|
sharedPreferencesName |
String : The name of the SharedPreferences file. |
setStorageDefault
void setStorageDefault ()
Sets the storage location used internally by this class to be the default
provided by the hosting Context
.
setStorageDeviceProtected
void setStorageDeviceProtected ()
Explicitly set the storage location used internally by this class to be device-protected storage.
On devices with direct boot, data stored in this location is encrypted with a key tied to the physical device, and it can be accessed immediately after the device has booted successfully, both before and after the user has authenticated with their credentials (such as a lock pattern or PIN).
Because device-protected data is available without user authentication, you should carefully limit the data you store using this Context. For example, storing sensitive authentication tokens or passwords in the device-protected area is strongly discouraged.
Prior to API 24 this method has no effect, since device-protected storage is not available.
See also:
showDialog
void showDialog (Preference preference)
Called when a preference requests that a dialog be shown to complete a user interaction.
Parameters | |
---|---|
preference |
Preference : The preference requesting the dialog.
|
Interfaces
- DialogPreference.TargetFragment
- Preference.OnPreferenceChangeListener
- Preference.OnPreferenceClickListener
- PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback
- PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
- PreferenceFragmentCompat.OnPreferenceStartScreenCallback
- PreferenceGroup.PreferencePositionCallback
- PreferenceManager.OnDisplayPreferenceDialogListener
- PreferenceManager.OnNavigateToScreenListener
- PreferenceManager.OnPreferenceTreeClickListener
Classes
- CheckBoxPreference
- DialogPreference
- DropDownPreference
- EditTextPreference
- EditTextPreferenceDialogFragmentCompat
- ListPreference
- ListPreferenceDialogFragmentCompat
- MultiSelectListPreferenceDialogFragmentCompat
- Preference
- Preference.BaseSavedState
- PreferenceCategory
- PreferenceDataStore
- PreferenceDialogFragmentCompat
- PreferenceFragmentCompat
- PreferenceGroup
- PreferenceManager
- PreferenceManager.PreferenceComparisonCallback
- PreferenceManager.SimplePreferenceComparisonCallback
- PreferenceScreen
- PreferenceViewHolder
- R
- R.anim
- R.attr
- R.bool
- R.color
- R.dimen
- R.drawable
- R.id
- R.integer
- R.layout
- R.string
- R.style
- R.styleable
- SeekBarPreference
- SwitchPreferenceCompat
- TwoStatePreference
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.