AbAppletic AbAppletic - 3 months ago 18
Android Question

What does the String parameter in getSharedPreferences() do?

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
we use a key?


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);

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()).