Skip to content Skip to sidebar Skip to footer

Difference Between Preference And Shared Preference In Android

The concept of preferences and shared preferences in Android are mixed up for me. What is the difference?

Solution 1:

Preferences: The user interface part of the settings. It contains different classes which allow to composes Settings screens from code or XML. They can look like this:

Image

Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you. They are not encrypted and can easily be changed when the user has rooted his/her phone (nice for development). Don't use these for sensitive information. The above mentioned Preferences use Shared Preferences as the underlying system.

Solution 2:

What the documentation is saying:

  • android.preference : is a package providing classes for preferences management ... The PreferenceScreen contains Preference elements such as a CheckBoxPreference, EditTextPreference, ListPreference, PreferenceCategory, or RingtonePreference... which means that preference is just the UI tools.

  • All settings made for a given Preference will be automatically saved to the application's instance of SharedPreferences. Access to the SharedPreferences is simple with getSharedPreferences()... which means that this is the way to save these preferences ...

Solution 3:

To understand it in a simple way-

SharedPreferences is an interface that manages a set of Preferences. SharedPreferences are stored as key-value pairs and updated in memory as user interacts with them. For eg. Brightness is a Preference in Display Settings.

To get hold of all Preferences we use SharedPreferences as

SharedPreferencessharedPreferences= getPreferenceScreen().getSharedPreferences();

whereas to handle a particular Preference we use

Preferencep= getPreferenceScreen().getPreference(index);

Post a Comment for "Difference Between Preference And Shared Preference In Android"