MPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE) You create the shared preferences file in the onCreate() method of your main activity and store it in a member variable. This makes its name unique and easily associated with your app. You need only one shared preferences file for your app, and it is customarily named with the package name of your app. For more information on settings and the Preference APIs, see Settings. The Preference APIs can be used to build a user interface for a settings page, and they use shared preferences for their underlying implementation. Note: The SharedPreference APIs are different from the Preference APIs. Represented by a small number of key/value pairs.Ĭommon use is to recreate state after the device has been rotated. Used for data that should not be remembered across sessions, such as the currently selected tab, or any current state of an activity. Used for data that should be remembered across user sessions, such as a user's preferred settings or their game score. Preserves state data across activity instances in the same user session. Persists across user sessions, even if your app is stopped and restarted, or if the device is rebooted. Here is a comparison between the two: Shared preferences In a previous chapter you learned about preserving state using saved instance states. The file is accessible to all the components of your app, but it is not accessible to other apps.įor managing large amounts of data, use an SQLite database or other suitable storage option, which is discussed in a later chapter. ![]() The Android framework manages the shared preferences file itself. ![]() To get a handle to a preference file, and to read, write, and manage preference data, use the SharedPreferences class. ![]() Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device. Lesson 3: Testing, debugging, and using support libraries
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |