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.
Related
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.
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.
1) What is the difference between
PreferenceManager.getDefaultSharedPreferences(context)
and
getSharedPreferences(name, mode)
2) And what does each of them do?
3) And how can I make a setting screen (Activity)?
1) You can have multiple SharedPreference files (so they are called SharedPreferences). The argument name of method getSharedPreferences(name, mode) specifies the the name of SharedPreference file to handle.
PreferenceManager.getDefaultSharedPreferences(context) returns the default SharedPreference file having default name and mode. Default name is based on your app's package name (as packagename_preferences.xml) and default mode is MODE_PRIVATE.
If you just want to use a single SharedPreferences file, PreferenceManager.getDefaultSharedPreferences(context) is concise to use.
2) With SharedPreferences you can save some key-value data.
3) Your last question: how to make a setting screen? is too wide topic to answer here. However, I suggest that using PreferenceActivity or PreferenceFragment you can manage a SharedPreferences without handling SharedPreferences directly.
(I'm Coming from a C# background writing iPhone apps with MonoTouch)
I have a simple app that needs to save button color. I have this code in a Settings class that is a singleton.
Is this the way it 'should' be done? I usually like having a single Setting instance that is accessed around the app in my C# iPhone apps.
private static Bundle bundle = new Bundle();
public static int getButtonColor() {
return bundle.getInt("buttonColor", RED_BUTTON) ;
}
public static void setButtonColor(int SettingButtonColor) {
bundle.putInt("buttonColor", SettingButtonColor);
}
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.
You probably want to look at setting SharedPreferences
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...)