1

I am using the Preference-API..
Typically when I need to retrieve the value of a Preference, I currently do something like this:

final SharedPreferences getPrefs = 
    PreferenceManager.getDefaultSharedPreferences(getActivity());

boolean isThisPrefEnabled = getPrefs.getBoolean(REFERENCE_TO_PREF_NAME, false);
//  OR
String theChosenPref = getPrefs.getString(PREF_NAME, DEFAULT_VALUE);

But I'm curious, couldn't I also do it like this? and if so, what is the difference?

Preference nameOfPref = findPreference(PREFERENCE_KEY);

boolean isPrefEnabled = nameOfPref.isEnabled();
//  OR
String thePrefValue = nameOfPref.toString();

It seems to be more efficient, but the first example seems to be what get's used. Why is this?
Thanks.

Studio2bDesigns
  • 578
  • 5
  • 12
  • The first one reads the saved data of prefs. The second one returns you the value of the object that is connected to the specific key. If you want to read the saved data anyware in your app, use the first one. If you want to know about the value that is selected while you are in pref screen, use the second. – Christos Themelis Mar 08 '19 at 11:55
  • possible duplicate.. https://stackoverflow.com/questions/23222931/difference-between-preference-and-shared-preference-in-android – Jakir Hossain Mar 08 '19 at 11:58

1 Answers1

0

SharedPreferences is an android specific interface documentation

android.content.SharedPreferences : is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.

Preferences is a core java class documentation

java.util.prefs.Preferences : This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store.

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37