PreferenceManager
public class PreferenceManager
Used to help create Preference hierarchies from activities or XML.
In most cases, clients should use addPreferencesFromResource.
| See also | |
|---|---|
PreferenceFragmentCompat |
Summary
Nested types |
|---|
public interface PreferenceManager.OnDisplayPreferenceDialogListenerInterface definition for a class that will be called when a |
public interface PreferenceManager.OnNavigateToScreenListenerInterface definition for a class that will be called when a |
public interface PreferenceManager.OnPreferenceTreeClickListenerInterface definition for a callback to be invoked when a |
public abstract class PreferenceManager.PreferenceComparisonCallbackCallback class to be used by the |
public class PreferenceManager.SimplePreferenceComparisonCallback extends PreferenceManager.PreferenceComparisonCallbackA basic implementation of |
Constants |
|
|---|---|
static final String |
KEY_HAS_SET_DEFAULT_VALUES = "_has_set_default_values" |
Public methods |
|
|---|---|
@NonNull PreferenceScreen |
createPreferenceScreen(@NonNull Context context) |
@Nullable T |
<T extends Preference> findPreference(@NonNull CharSequence key)Finds a |
@NonNull Context |
Returns the context. |
static SharedPreferences |
getDefaultSharedPreferences(@NonNull Context context)Gets a |
@Nullable PreferenceManager.OnDisplayPreferenceDialogListener |
|
@Nullable PreferenceManager.OnNavigateToScreenListener |
Returns the |
@Nullable PreferenceManager.OnPreferenceTreeClickListener |
|
@Nullable PreferenceManager.PreferenceComparisonCallback |
|
@Nullable PreferenceDataStore |
Returns the |
PreferenceScreen |
Returns the root of the preference hierarchy managed by this class. |
@Nullable SharedPreferences |
Gets a |
int |
Returns the current mode of the |
String |
Returns the current name of the |
boolean |
Indicates if the storage location used internally by this class is the default provided by the hosting |
boolean |
Indicates if the storage location used internally by this class is backed by device-protected storage. |
static void |
setDefaultValues(@NonNull Context context, int resId, boolean readAgain)Sets the default values from an XML preference file by reading the values defined by each |
static void |
setDefaultValues(Similar to |
void |
setOnDisplayPreferenceDialogListener( |
void |
Sets the callback to be invoked when a |
void |
setOnPreferenceTreeClickListener(Sets the callback to be invoked when a |
void |
setPreferenceComparisonCallback( |
void |
setPreferenceDataStore(@Nullable PreferenceDataStore dataStore)Sets a |
boolean |
setPreferences(PreferenceScreen preferenceScreen)Sets the root of the preference hierarchy. |
void |
setSharedPreferencesMode(int sharedPreferencesMode)Sets the mode of the |
void |
setSharedPreferencesName(String sharedPreferencesName)Sets the name of the |
void |
Sets the storage location used internally by this class to be the default provided by the hosting |
void |
Explicitly set the storage location used internally by this class to be device-protected storage. |
void |
showDialog(@NonNull Preference preference)Called when a preference requests that a dialog be shown to complete a user interaction. |
Constants
KEY_HAS_SET_DEFAULT_VALUES
public static final String KEY_HAS_SET_DEFAULT_VALUES = "_has_set_default_values"
Public methods
createPreferenceScreen
public @NonNull PreferenceScreen createPreferenceScreen(@NonNull Context context)
findPreference
public @Nullable T <T extends Preference> findPreference(@NonNull CharSequence key)
Finds a Preference with the given key. Returns null if no Preference could be found with the given key.
| Parameters | |
|---|---|
@NonNull CharSequence key |
The key of the |
| Returns | |
|---|---|
@Nullable T |
The |
| See also | |
|---|---|
findPreference |
getDefaultSharedPreferences
public static SharedPreferences getDefaultSharedPreferences(@NonNull Context context)
Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.
| Returns | |
|---|---|
SharedPreferences |
A |
getOnDisplayPreferenceDialogListener
public @Nullable PreferenceManager.OnDisplayPreferenceDialogListener getOnDisplayPreferenceDialogListener()
getOnNavigateToScreenListener
public @Nullable PreferenceManager.OnNavigateToScreenListener getOnNavigateToScreenListener()
Returns the PreferenceManager.OnNavigateToScreenListener, if one has been set.
getOnPreferenceTreeClickListener
public @Nullable PreferenceManager.OnPreferenceTreeClickListener getOnPreferenceTreeClickListener()
getPreferenceComparisonCallback
public @Nullable PreferenceManager.PreferenceComparisonCallback getPreferenceComparisonCallback()
getPreferenceDataStore
public @Nullable PreferenceDataStore getPreferenceDataStore()
Returns the PreferenceDataStore associated with this manager or null if the default SharedPreferences are used instead.
| Returns | |
|---|---|
@Nullable PreferenceDataStore |
The |
| See also | |
|---|---|
setPreferenceDataStore |
getPreferenceScreen
public PreferenceScreen getPreferenceScreen()
Returns the root of the preference hierarchy managed by this class.
| Returns | |
|---|---|
PreferenceScreen |
The |
getSharedPreferences
public @Nullable SharedPreferences getSharedPreferences()
Gets a SharedPreferences instance that preferences managed by this will use.
| Returns | |
|---|---|
@Nullable SharedPreferences |
A |
getSharedPreferencesMode
public 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 |
| See also | |
|---|---|
getSharedPreferences |
getSharedPreferencesName
public 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 |
| See also | |
|---|---|
getSharedPreferences |
isStorageDefault
public boolean isStorageDefault()
Indicates if the storage location used internally by this class is the default provided by the hosting Context.
isStorageDeviceProtected
public boolean isStorageDeviceProtected()
Indicates if the storage location used internally by this class is backed by device-protected storage.
setDefaultValues
public static void setDefaultValues(@NonNull 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 | |
|---|---|
@NonNull Context context |
The context of the shared preferences |
int resId |
The resource ID of the preference XML file |
boolean 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 Note: this will NOT reset preferences back to their default values. For that functionality, use |
setDefaultValues
public static void setDefaultValues(
@NonNull Context context,
String sharedPreferencesName,
int sharedPreferencesMode,
int resId,
boolean readAgain
)
Similar to setDefaultValues but allows the client to provide the filename and mode of the shared preferences file.
| Parameters | |
|---|---|
@NonNull Context context |
The context of the shared preferences |
String sharedPreferencesName |
A custom name for the shared preferences file |
int sharedPreferencesMode |
The file creation mode for the shared preferences file, such as |
int resId |
The resource ID of the preference XML file |
boolean 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 Note: this will NOT reset preferences back to their default values. For that functionality, use |
setOnDisplayPreferenceDialogListener
public void setOnDisplayPreferenceDialogListener(
@Nullable PreferenceManager.OnDisplayPreferenceDialogListener onDisplayPreferenceDialogListener
)
setOnNavigateToScreenListener
public void setOnNavigateToScreenListener(
@Nullable PreferenceManager.OnNavigateToScreenListener listener
)
Sets the callback to be invoked when a PreferenceScreen in the hierarchy rooted at this PreferenceManager is clicked.
| Parameters | |
|---|---|
@Nullable PreferenceManager.OnNavigateToScreenListener listener |
The callback to be invoked |
setOnPreferenceTreeClickListener
public void setOnPreferenceTreeClickListener(
@Nullable PreferenceManager.OnPreferenceTreeClickListener listener
)
Sets the callback to be invoked when a Preference in the hierarchy rooted at this PreferenceManager is clicked.
| Parameters | |
|---|---|
@Nullable PreferenceManager.OnPreferenceTreeClickListener listener |
The callback to be invoked |
setPreferenceComparisonCallback
public void setPreferenceComparisonCallback(
@Nullable PreferenceManager.PreferenceComparisonCallback preferenceComparisonCallback
)
setPreferenceDataStore
public void setPreferenceDataStore(@Nullable 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. Also if the data store is set, the child preferences won't use SharedPreferences as long as they are assigned to this manager.
| Parameters | |
|---|---|
@Nullable PreferenceDataStore dataStore |
The |
| See also | |
|---|---|
setPreferenceDataStore |
setPreferences
public boolean setPreferences(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy.
| Parameters | |
|---|---|
PreferenceScreen preferenceScreen |
The root |
| Returns | |
|---|---|
boolean |
Whether the |
setSharedPreferencesMode
public void setSharedPreferencesMode(int sharedPreferencesMode)
Sets the mode of the SharedPreferences file that preferences managed by this will use.
| Parameters | |
|---|---|
int sharedPreferencesMode |
The mode of the SharedPreferences file |
| See also | |
|---|---|
getSharedPreferences |
setSharedPreferencesName
public 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 | |
|---|---|
String sharedPreferencesName |
The name of the SharedPreferences file |
setStorageDefault
public void setStorageDefault()
Sets the storage location used internally by this class to be the default provided by the hosting Context.
setStorageDeviceProtected
public 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 | |
|---|---|
createDeviceProtectedStorageContext |
showDialog
public void showDialog(@NonNull Preference preference)
Called when a preference requests that a dialog be shown to complete a user interaction.
| Parameters | |
|---|---|
@NonNull Preference preference |
The preference requesting the dialog |