What is the difference between Preference and SharedPreference?

5

Looking at some forms of data persistence in Android, I noticed that you have two very similar interfaces PreferenceChangeListener and SharedPreferenceChangeListener , which can be extended in Preference and SharedPreference classes.

What difference between Preference and SharedPreference and their respective interfaces mentioned above?

    
asked by anonymous 30.03.2017 / 18:40

1 answer

2

SharedPreference

It is a system that allows you to access and store, persistently, values referenced by a key.

Access to the system is achieved through Context#getSharedPreferences() or Activity#getPreferences() methods, which return an object that implements the SharedPreferences .

One possible use is to store application state values at the time the user left the application so that it can be retrieved when the user returns to the state.

OnSharedPreferenceChangeListener .

Interface to be implemented by a callback to be invoked when a SharedPreference is changed. The callback must be registered using the registerOnSharedPreferenceChangeListener() method.

Preference

It is a system that allows you to unify how applications allow users to configure application characteristics and behaviors.

To maintain an interface consistent with the user experience with other Android applications, subclasses of Preference , one for each value type, to be used as views in a PreferenceActivity or PreferenceFragment.

The Preference class uses SharedPreference to persist its values.

OnPreferenceChangeListener .

Interface to be implemented by a callback to be invoked when this Preference is changed. The callback is associated with Preference using the setOnPreferenceChangeListener() method.

References

31.03.2017 / 00:16