public class PreferenceManager extends Object
Preference
hierarchies
from activities or XML.
In most cases, clients should use
PreferenceFragment.addPreferencesFromResource(int)
, or
PreferenceFragmentCompat.addPreferencesFromResource(int)
.
PreferenceFragment
,
PreferenceFragmentCompat
Modifier and Type | Class and Description |
---|---|
static interface |
PreferenceManager.OnDisplayPreferenceDialogListener
Interface definition for a class that will be called when a
Preference requests to display a dialog. |
static interface |
PreferenceManager.OnNavigateToScreenListener
Interface definition for a class that will be called when a
PreferenceScreen requests navigation. |
static interface |
PreferenceManager.OnPreferenceTreeClickListener
Interface definition for a callback to be invoked when a
Preference in the hierarchy rooted at this PreferenceScreen is
clicked. |
Modifier and Type | Field and Description |
---|---|
static String |
KEY_HAS_SET_DEFAULT_VALUES |
Constructor and Description |
---|
PreferenceManager(Context context) |
Modifier and Type | Method and Description |
---|---|
PreferenceScreen |
createPreferenceScreen(Context context) |
Preference |
findPreference(CharSequence key)
Finds a
Preference based on its key. |
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.OnNavigateToScreenListener , if one has been set. |
PreferenceManager.OnPreferenceTreeClickListener |
getOnPreferenceTreeClickListener() |
PreferenceScreen |
getPreferenceScreen()
Returns the root of the preference hierarchy managed by this class.
|
SharedPreferences |
getSharedPreferences()
Gets a SharedPreferences instance that preferences managed by this will
use.
|
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 SharedPreferences file that preferences managed by
this will use.
|
PreferenceScreen |
inflateFromResource(Context context,
int resId,
PreferenceScreen rootPreferences)
Inflates a preference hierarchy from XML.
|
boolean |
isStorageDefault()
Indicates if the storage location used internally by this class is the
default provided by the hosting
Context . |
boolean |
isStorageDeviceProtected()
Indicates if the storage location used internally by this class is backed
by device-protected storage.
|
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
Preference item's android:defaultValue attribute. |
static 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. |
void |
setOnDisplayPreferenceDialogListener(PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener) |
void |
setOnNavigateToScreenListener(PreferenceManager.OnNavigateToScreenListener listener)
Sets the callback to be invoked when a
PreferenceScreen in the hierarchy rooted at
this PreferenceManager is clicked. |
void |
setOnPreferenceTreeClickListener(PreferenceManager.OnPreferenceTreeClickListener listener)
Sets the callback to be invoked when a
Preference in the
hierarchy rooted at this PreferenceManager is clicked. |
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 SharedPreferences file that preferences managed by this
will use.
|
void |
setStorageDefault()
Sets the storage location used internally by this class to be the default
provided by the hosting
Context . |
void |
setStorageDeviceEncrypted()
Deprecated.
|
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.
|
public static final String KEY_HAS_SET_DEFAULT_VALUES
public PreferenceManager(Context context)
public PreferenceScreen inflateFromResource(Context context, int resId, PreferenceScreen rootPreferences)
context
- The context of the resource.resId
- The resource ID of the XML to inflate.rootPreferences
- Optional existing hierarchy to merge the new
hierarchies into.public PreferenceScreen createPreferenceScreen(Context context)
public String getSharedPreferencesName()
Context.getSharedPreferences(String, int)
.Context.getSharedPreferences(String, int)
public void setSharedPreferencesName(String sharedPreferencesName)
sharedPreferencesName
- The name of the SharedPreferences file.Context.getSharedPreferences(String, int)
public int getSharedPreferencesMode()
Context.getSharedPreferences(String, int)
.Context.getSharedPreferences(String, int)
public void setSharedPreferencesMode(int sharedPreferencesMode)
sharedPreferencesMode
- The mode of the SharedPreferences file.Context.getSharedPreferences(String, int)
public void setStorageDefault()
Context
.public void setStorageDeviceProtected()
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 BuildCompat.isAtLeastN()
this method has no effect,
since device-protected storage is not available.
@Deprecated public void setStorageDeviceEncrypted()
public boolean isStorageDefault()
Context
.setStorageDefault()
,
setStorageDeviceProtected()
public boolean isStorageDeviceProtected()
setStorageDefault()
,
setStorageDeviceProtected()
public SharedPreferences getSharedPreferences()
public static SharedPreferences getDefaultSharedPreferences(Context context)
context
- The context of the preferences whose values are wanted.public PreferenceScreen getPreferenceScreen()
PreferenceScreen
object that is at the root of the hierarchy.public boolean setPreferences(PreferenceScreen preferenceScreen)
preferenceScreen
- The root PreferenceScreen
of the preference hierarchy.PreferenceScreen
given is different than the previous.public Preference findPreference(CharSequence key)
Preference
based on its key.key
- The key of the preference to retrieve.Preference
with the key, or null.PreferenceGroup.findPreference(CharSequence)
public static void setDefaultValues(Context context, int resId, boolean readAgain)
Preference
item's android:defaultValue
attribute. This should
be called by the application's main activity.
context
- The context of the shared preferences.resId
- The resource ID of the preference XML file.readAgain
- 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
getDefaultSharedPreferences(Context)
and clear it followed by a call to this method with this
parameter set to true.
public static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
setDefaultValues(Context, int, boolean)
but allows
the client to provide the filename and mode of the shared preferences
file.context
- The context of the shared preferences.sharedPreferencesName
- A custom name for the shared preferences file.sharedPreferencesMode
- The file creation mode for the shared preferences file, such
as Context.MODE_PRIVATE
or Context.MODE_PRIVATE
resId
- The resource ID of the preference XML file.readAgain
- 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
getDefaultSharedPreferences(Context)
and clear it followed by a call to this method with this
parameter set to true.
setDefaultValues(Context, int, boolean)
,
setSharedPreferencesName(String)
,
setSharedPreferencesMode(int)
public Context getContext()
public PreferenceManager.OnDisplayPreferenceDialogListener getOnDisplayPreferenceDialogListener()
public void setOnDisplayPreferenceDialogListener(PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener)
public void showDialog(Preference preference)
preference
- The preference requesting the dialog.public void setOnPreferenceTreeClickListener(PreferenceManager.OnPreferenceTreeClickListener listener)
Preference
in the
hierarchy rooted at this PreferenceManager
is clicked.listener
- The callback to be invoked.public PreferenceManager.OnPreferenceTreeClickListener getOnPreferenceTreeClickListener()
public void setOnNavigateToScreenListener(PreferenceManager.OnNavigateToScreenListener listener)
PreferenceScreen
in the hierarchy rooted at
this PreferenceManager
is clicked.listener
- The callback to be invoked.public PreferenceManager.OnNavigateToScreenListener getOnNavigateToScreenListener()
PreferenceManager.OnNavigateToScreenListener
, if one has been set.