0

Edit: The provided "duplicate" doesn't solve my question as it doesn't fully answer my question. (Info about the second parameter is missing).


This question is intended to clear out the information for new Android developers and not to solve my own problem. Please reconsider the downvotes.

So, here's the method:

getSharedPreferences(string, Context.MODE_PRIVATE);

I can't really get what the first parameter does. What does it do? Why is there a first parameter if when we save something to SharedPreferences we use a key?

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • https://developer.android.com/training/basics/data-storage/shared-preferences.html#GetSharedPreferences – Mohammed Atif Oct 20 '16 at 17:49
  • @MohammedAtif I knew there was an answer there, but I wanted to help future readers who want a clearer answer. :) – Ali Bdeir Oct 20 '16 at 17:50
  • Possible duplicate of [Android SharedPreferences with MODE\_PRIVATE,MODE\_WORLD\_READABLE,MODE\_WORLD\_WRITABLE](http://stackoverflow.com/questions/13153231/android-sharedpreferences-with-mode-private-mode-world-readable-mode-world-writa) – Mohammed Atif Oct 20 '16 at 17:51
  • @MohammedAtif NOT a duplicate, that question doesn't sufficiently answer my question as it doesn't concern the second parameter – Ali Bdeir Oct 20 '16 at 17:52
  • I am not sure whether you are concern about the reader or SO points. It is clearly stated that sharedPreferences are **key-value** method of storing apps data into a file (your second parameter) which can be shared with other apps or kept private for the app – Mohammed Atif Oct 20 '16 at 17:54
  • @MohammedAtif you clearly didn't read the question since you **are missing the second parameter**. Link me to the answer that includes that, and I'd happily close this as a duplicate. Otherwise, you have no rights to say I'm doing this for SO points since you yourself can't find out why. – Ali Bdeir Oct 20 '16 at 17:55
  • second parameter is the name of the file where shared preferences are saved – Mohammed Atif Oct 20 '16 at 17:56
  • @MohammedAtif Then put it as an answer. If you find an answer in another post, flag THAT as a duplicate and I'll happily close it too. – Ali Bdeir Oct 20 '16 at 17:57
  • **This isn't a duplicate, the link doesn't answer my question (see the edit). Link me to the answer and I'll happily close it as a duplicate.** – Ali Bdeir Oct 20 '16 at 17:58
  • http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences – Mohammed Atif Oct 20 '16 at 18:05
  • @MohammedAtif I'm not sure how that is related to my question. – Ali Bdeir Oct 20 '16 at 18:06
  • well, i cant help further, i gave you 3 links which can answer your question. I agree one is not exactly relavant, but remaining two clearly explains the working of getsharedpreference. please read them again. – Mohammed Atif Oct 20 '16 at 18:12
  • @MohammedAtif after re-reading, I noticed that it, in a very indirect way, does answer my question. However, clearer, more direct answers are recommended. – Ali Bdeir Oct 20 '16 at 18:13
  • @MohammedAtif are you going to post an answer? Otherwise I will. – Ali Bdeir Oct 20 '16 at 18:14
  • bro, both the links clearly say that getsharedpreference uses custom file (the first parameter) to store key value pairs with certain visibility (second parameter). And yea, you have inverted the parameters in your question. – Mohammed Atif Oct 20 '16 at 18:15
  • @MohammedAtif If I, an Android Developer for 1.5 years, didn't get it the first time, I don't think any newbie will. – Ali Bdeir Oct 20 '16 at 18:17

2 Answers2

6

As documented in the Android Developer Documentation for getSharedPreferences(), the full signature for the method is:

SharedPreferences getSharedPreferences (String name, int mode)

The formal signature provides the name of the first parameter, name, which is information useful to the answer. The name parameter is the base name (without file extension) of an XML preferences file located in the private storage of the app.

For example, this call will return the SharedPreferences instance to allow reading and writing the app's settings.xml preferences file:

SharedPreferences sharedPrefs = getSharedPreferences("settings", Context.MODE_PRIVATE);

As indicated in the official documentation, the returned SharedPreferences object is a single-instance object, shared between all callers for the same file name. This means that a given call does not necessarily imply file IO to read a given preference, but may incur thread synchronization between threads in the same app.

The specified file will be created if it doesn't already exist prior to calling getSharedPreferences(). The second argument, mode, is the mode to use when creating the file, and should be set to Context.MODE_PRIVATE (or it's integer value 0); other mode values are not documented as allowed, and should not be used. As when creating any file, indicating a mode of Context.MODE_PRIVATE will locate the file in the app's private storage, as is expected for use with getSharedPreferences().

An example of writing a value (999) to a key (setting) in a SharedPreferences instance is this:

Context context = getActivity();
SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putInt("setting", 999);
editor.apply();

Reading the value from the same key is done this way:

Context context = getActivity();
SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
sharedPrefs.getInt("setting", 0);

Additional usage information can be found in the Saving Key-Value Sets page in the Android Getting Started Guide.

Note that getSharedPreferences() is a generalized version of getPreferences(), which is often the better choice for common application preferences. Aside from the ability to specify which preferences file to use with getSharedPreferences(), both methods are otherwise identical in function and behavior. According to the getPreferences() documentation, it simply calls getSharedPreferences() with "this activity's class name as the preferences name" (the first parameter to getSharedPreferences()).

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
-1

The String parameter in getSharedPreferences() is the file name that stores the keys and values that you provide. For example:

SharedPreferences.Editor s = getSharedPreferences("Pref",Context.MODE_PRIVATE).edit();
s.putInt("someKey",0);
s.apply();

Will make an output file in your app, called Pref, which contains the keys you'll enter.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117