What is the difference between Preference and Sharedpreference?

Asked

Viewed 214 times

5

Seeing some forms of data persistence on Android, I realized that has two interfaces very similar PreferenceChangeListener and SharedPreferenceChangeListener, which can be extended in the classes Preference and SharedPreference.

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

  • Has a post in So-En about.

1 answer

2

Sharedpreference

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

Access to the system is obtained through the methods Context#getSharedPreferences() or Activity#getPreferences(), that return an object that implements the interface Sharedpreferences.

A possible use is to store values of the state of the application, at the time the user abandoned it, so that this state is recovered when he returns to it.

OnSharedPreferenceChangeListener.

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

Preference

It’s a system that lets you 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 type of value, to be used as views in a Preferenceactivity or a 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. callback is associated with Preference via the method setOnPreferenceChangeListener().

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.