Nouman Ghaffar
Nouman Ghaffar

Reputation: 233

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?

Upvotes: 23

Views: 10846

Answers (3)

Ananth
Ananth

Reputation: 2735

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

 SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();

whereas to handle a particular Preference we use

Preference p = getPreferenceScreen().getPreference(index);

Upvotes: 2

tilpner
tilpner

Reputation: 4338

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.

Upvotes: 23

ahmed_khan_89
ahmed_khan_89

Reputation: 2773

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 ...

Upvotes: 14

Related Questions