What does the String parameter in getSharedPreferences() do? - android

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?

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

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.

Related

How to make shared-preferences globally available in android application?

I want to get some key-values throughout my application, in iOS we have user-defaults, how can we make preferences behave like that i.e. accessible from any activity?
You can create a new shared preference file or access an existing one by calling one of these methods:
getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
For example, the following code accesses the shared preferences file that's identified by the resource string R.string.preference_file_key and opens it using the private mode so the file is accessible by only your app:
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
You can read the official guide on Save key-value data.

Android: PreferenceManager vs Context.getSharedPreferences() and why the latter failed me

ok, following the Udacity Android Development Course, I reached the part where I'm expected to access the postal code saved to a sharedPreferences file called pref_general.xml, which has the postal code saved in string type and connected to a key called location via key-value pair.
My approach to the problem was to use the getSharedPreferences() method to get the file by name. While that appears to not cause problems because the file didn't turn out null, the attempt to retrieve the postal code resulted in not finding the value via the key and settling on the given default value of the argument.
SharedPreferences appPreferences = getActivity().getSharedPreferences("pref_general", Context.MODE_PRIVATE);
if(appPreferences == null) {
Log.v("ERRORTAG", "Cannot get sharedPreferences file");
}
String getPostal = appPreferences.getString(getString(R.string.pref_location_key), "0");
Log.v("ERRORTAG", getPostal);
The 2nd verbose statement on logcat results in the default String value of 0 instead of the value tied to the given key of 94043 postal code.
Now the answer Udacity gave was to use PreferenceManager, which grabs the default lone sharedPreferences file tied to the Activity
SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
The file isn't null AND the retrieved postal code is the given default postal code 94043 set within the sharedPreference file as a key-value pair.
I want to understand why my approach wasn't working; it was quite close. The only difference was how the file was accessed. Please give me an explanation as to why. Thank you.
The docs for PreferenceActivity say:
If you are using PreferenceActivity in its old mode, the documentation [for PreferenceFragment] applies to the deprecated APIs here.
And the docs for PreferenceFragment explain what's happening:
To retrieve an instance of SharedPreferences that the preference hierarchy in this fragment will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this fragment.
This suggests that PreferenceActivity#addPreferencesFromResource(...) does not create a SharedPreferences file with the same name as the original. Instead, it merges the file into the default shared preferences. The pref_general file does not exist, and you're basically creating it when you attempt to read from it. (Though it's not actually created on disk until you edit it.)

PreferenceManager.getDefaultSharedPreferences() vs getPreferences()

PreferenceManager.getDefaultSharedPreferences(context)
and
getPreferences()
seem to retrieve different Preferences.
PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
"userWasAskedToEnableGps", false);
for me returns false,
getPreferences(MODE_PRIVATE).getBoolean("userWasAskedToEnableGps", false);
returns true.
The Preference was written with an Editor like
Editor e = getPreferences(MODE_PRIVATE).edit();
e.putBoolean (...);
e.commit();
How can I get the same Preferences outside of an Activity from the Context?
According to the docs
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name. scope limited to activity where it is created
So if you want the preference to be available in another activity or context, you have to use either of getSharedPreferences() or getDefaultSharedPreferences()
This ans explains you the difference between the two.
From android github repo(1), we can see that getPreferences does nothing other than invoking getSharedPreferences method with current class name.
public SharedPreferences getPreferences( int mode ) {
return getSharedPreferences( getLocalClassName(), mode );
}
There is nothing limiting other activities/code from accessing the shared preference with appropriate class name. More importantly, I prefer not to use getPreferences, since that implies => never ever change the Activity name. If you change, then take care of the accessing shared preferences with explicit mentions to the earlier class name ( before upgrade ).
Use Context.getSharedPreferences with the same constant name and it will give you the same preferences in any point of your app.

How to get contents of SharedPreferences file and overwrite them?

I am pretty new to Android dev. I am browsing through the API here http://developer.android.com/reference/android/content/SharedPreferences.html
but I am confused about how to actually get the file contents and read or write from them.
I have this code to get the SharedPreferences object:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( MyClassName.this);
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
Thanks!
If you want to get values from your SharedPreferneces you have to use (this example works with Strings, but you can also call getBoolean, getInt, etc..)
prefs.getString("myString", "defaultValue"); // "defaultValue" will be returned in case "myString" wasn't saved on the SharedPreferences
to store some values you can do it like this:
prefs.edit()
.putString("myString", "newValue")
.putBoolean("working", true)
.commit();
As you can see you can edit more than one value at once..
edit() will return you an editor that you have to use in order to modify the sharedpreferences file, and when you end to edit it call commit() in order to make the changes permanently
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
The SharedPreferences object has a "connection" to the file which stores the preference data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
To read preferences, use the getters on SharedPreferences (e.g., getString()). To write preferences yourself:
Get a SharedPreferences.Editor by calling edit() on the SharedPreferences object
Use the setters on the Editor (e.g., putString())
Call apply() (where possible) or commit() on the Editor to save your changes
In addition, you can (and in many cases should) also use a PreferenceActivity to allow users to directly view and modify their preferences.

saving state of an android Application?

Which of the following is/are appropriate for saving the state of an Android application?
a. Activity.onFreeze()
b. Activity.onPause()
c. Activity.onStop()
d. Activity.onDestroy()
e. Activity.onFinish()
Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.
Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.
Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.
Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.
Shared Preferences:
The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.
(1) Here is how you get the instance when you specify the file name
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.
(2) The recommended way is to use by the default mode, without specifying the file name
SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);
Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:
int storedPreference = preferences.getInt("storedInt", 0);
To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
Editor also support methods like remove() and clear() to delete the preference value from the file.
Activity Preferences:
The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.
Following is the code to get preferences
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);
The code to store values is also same as in case of shared preferences.
SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.
To see some more examples check Android's Data Storage post on developers site.
Why won't you just use SharedPreferences?
Anyways: i'd store the data in onPause(), depending on what these data items are. You probably already know that all items that have an id assigned in the R.java are automatically saved onPause() and restored onResume()? - but this is more or less useless anyways since this data is lost after the app dies completely...
assuming that you want so save other stuff that you might want to write to an DB or file, onPause() might be a good choice. as you can see in http://xenonite.net/news/android-activity-lifecycle, after onPause() is called, the OS might kill the app if there's memory needed for other things. so the data will be lost if you'd try to save it somewhere else (e.g. onStop())
but: make sure, that this saving produces not too much overhead, since onPause() is called on several occasions (e.g. screen rotates...)

Categories

Resources