11

I've come across two classes being used in a tutorial on splash screens PreferenceManager and SharedPreferences. I didn't gain a great deal of knowledge about them from the tutorial though.

So can someone explain to me what both classes do or are used for?

Schonge
  • 332
  • 2
  • 6
  • 17
  • 1
    They are used to store and retrieve the **settings** for your app. It's like having some variables stored in a file along with their values. These are in fact called **key/value pairs**. – Phantômaxx Mar 04 '14 at 17:11
  • 1
    Be advised; these values are lost when you uninstall the application. They are normally kept safe during an update; but 'due to some "unknown" problem, the data may get lost' – Unknownweirdo Mar 04 '14 at 17:17

4 Answers4

17

From the Android Developer site:

PreferenceManager:

Used to help create Preference hierarchies from activities or XML.

SharedPreferences:

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share.

Put simply, PreferenceManager is normally used when you want to create a PreferenceActivity or load in some Preferences from an .xml file in your application with default values, and holds it's own referenced to SharedPreferences.

SharedPreferences is where you handle the storing and retrieving of key/value pairs that make up your preferences. So you can add variables with keys to retrieve the data later. This feeds into the PreferenceManager which can handle adding default values and setting up the default SharedPreferences.

You can use SharedPreferences throughout your application without needing to use PreferenceManager, but the opposite isn't strictly true.

Further reading:

Community
  • 1
  • 1
biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • 2
    This is a more concise answer to the original question than the accepted answer - thank you. – Mark Aug 08 '16 at 10:27
10

Preferences is an Android lightweight mechanism to store and retrieve pairs of primitive data types (also called Maps, and Associative Arrays).

In each entry of the form the key is a string and the value must be a primitive data type.

WHEN WE NEED THEM:

PREFERENCES are typically used to keep state information and shared data among several activities of an application.

Shared Preferences is the storage, in android, that you can use to store some basic things related to functionality, users' customization or its profile.

Suppose you want to save user's name in your app for future purposes. You cant save such a little thing in database, So you better keep it saved in your Preferences. Preferences is just like a file , from which you can retrieve value anytime in application's lifetime in a KEY-VALUE pair manner.

Take another example, If you use whatsapp, we have a wallpaper option there. How the application knows which image serves as wall-paper for you whenever you open your whatsapp. This information is stored in preferences. Whenever you clear data for any app, preferences are deleted.

HOW TO USE THESE PREFERENCES :

final int mode = Activity.MODE_PRIVATE; 
final String MYPREFS = "MyPreferences_001"; 

// create a reference to the shared preferences object 
SharedPreferences mySharedPreferences; 

// obtain an editor to add data to my SharedPreferences object 
SharedPreferences.Editor myEditor;

mySharedPreferences = getSharedPreferences(MYPREFS, 0); 

// using this instance you can get any value saved.
 mySharedPreferences.getInt("backColor",Color.BLACK); // default value is BLACK set here

EDITING SHARED PREFERENCES :

myEditor = mySharedPreferences.edit(); 
//edit and commit
myEditor.putString("backColor", Color.RED); 
myEditor.commit() //very imp.
Bharat
  • 904
  • 6
  • 19
1

As explained Artoo Detoo... Sharedpreferences kinda works like sessions in web development. you can use them to pass values from one activity to another and it stays that way as far as the app is in use except otherwise changed..

it is also use to user value (either after login or registration of the user). thats how much i can talk about it

1baga
  • 340
  • 4
  • 16
  • so it lets the app no when switching activities that the user is logged in otherwise it would reset itself to being logged out once it changes activity? Or for another example, the user sets the music off for the app and wants the app to remember that this will handle it? – Schonge Mar 04 '14 at 17:33
  • 1
    it never changes it value unless the user changes it...that is if the developer gives the user the power to... your second example answers it all.. – 1baga Mar 04 '14 at 21:41
1

SharedPreference APIs are used to save key-value pairs. They store them in files and are private or public based on the mode you instantiate the SharedPreference object. They are used to store a small set of key-value pairs. This key here is of type String and the value can be any primitive type.

PreferenceManager is part of the Preference APIs. Preference API allows you to define a complete settings UI. This settings UI is an XML layout. You use a PreferenceManager to manage this Preference object's tree. It uses the SharedPreference APIs to store the various settings a user might change using that graphical layout you created.

Reference - "Android Docs Training"

rupj
  • 327
  • 8
  • 14