![]() Note Android also contains a package called android.preference. You can also use other methods like storing the activity state in database. SharedPreferences.Editor editor = preferences.edit() SharedPreferences preferences = getPreference(MODE_PRIVATE) The code to store values is also the same as in case of shared preferences. Int storedPreference = preferences.getInt("storedInt", 0) The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.įollowing is the code to get preferences SharedPreferences preferences = getPreferences(MODE_PRIVATE) ![]() But if you do not need to share the preferences with other components and want to have activity private preferences you can do that with the help of getPreferences() method of the activity. The shared preferences can be used by other application components. SharedPreferences.Editor editor = preferences.edit() Įditor.putInt("storedInt", storedPreference) // value to storeĮditor also supports methods like remove() and clear() to delete the preference values from the file. Editor is a nested interface in the SharedPreference class. To store values in the preference file SharedPreference.Editor object has to be used. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.įinally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences: int storedPreference = preferences.getInt("storedInt", 0) ![]() In MODE_WORLD_READABLE other application can read the created file but can not modify it. Other two modes supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. It is the default mode and means the created file will be accessed by only the calling application. MODE_PRIVATE is the operating mode for the preferences. SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE) (2) Here is how you get the instance when you specify the file name public static final String PREF_FILE_NAME = "PrefFile" (1) The recommended way is to use by the default mode, without specifying the file name SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context) 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. The shared preferences are managed with the help of getSharedPreferences method of the Context class. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) of the applications.Īctivity handled preferences: These preferences can only be used within the particular activity and can not be used by other components of the application. ![]() Something that needs to be stored specific to an activity. “Shared Preferences” across various activities in an application (noteĬurrently it cannot be shared across processes). Preferences are typically name value pairs. This, Android provides a simple set of APIs. Settings of a specific application or an activity. Many applications may provide a way to capture user preferences on the SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this) Īccording to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N,
0 Comments
Leave a Reply. |