Many applications need a lightweight data storage mechanism called shared preferences for storing application state, simple user information, configuration options, and other such information.
Android provides a simple preferences system for storing primitive application data at the Activity level and preferences shared across all of an application’s activities. You cannot share preferences outside of the package. Preferences are stored as groups of key/value pairs. The following data types are supported as preference settings:
Preference functionality can be found in the SharedPreferences interface of the android. content package. To add preferences support to your application, you must take the following steps:
Creating Private and Shared Preferences
Individual activities can have their own private preferences. These preferences are for the specific Activity only and are not shared with other activities within the application. The activity gets only one group of private preferences.
The following code retrieves the activity’s private preferences:
Creating shared preferences is similar. The only two differences are that we must name our preference set and use a different call to get the preference instance:
You can access shared preferences by name from any activity in the application. There is no limit to the number of different shared preferences you can create. You can have some shared preferences called UserNetworkPreferences and another called AppDisplayPreferences. How you organize shared preferences is up to you, the developer. However, you want to declare your preference name as a variable (in a base class or header) so that you can reuse the name across multiple activities. For example
Searching and Reading Preferences
Reading preferences is straightforward. Simply retrieve the SharedPreferences instance you want to read. You can check for a preference by name, retrieve strongly typed preferences, and register to listen for changes to the preferences. Table describes some helpful methods in the SharedPreferences interface.
Important android.content.SharedPreferences Methods
Adding, Updating, and Deleting Preferences
To change preferences, you need to open the preference Editor, make your changes, and commit them. Table describes some helpful methods in the Shared Preferences. Editor interface.
Important android.content.SharedPreferences .Editor Methods
The following block of code retrieves the activity’s private preferences, opens the preference editor, adds a long preference called SomeLong, and saves the change:
Finding Preferences Data on the Android File System
Internally, application preferences are stored as XML files. You can access the preferences file using DDMS using the File Explorer. You find these files on the Android file system in the following directory:
The preferences filename is the Activity’s class name for private preferences or the name you give for the shared preferences. Here is an example of the file contents of a simple preference file with a preference in each data type:
Understanding the application preferences file format can be helpful for testing purposes. You can use Dalvik Debug Monitor Service (DDMS) to copy the preferences files to and from the device.
Setting Up Your Android Development Environment
Writing Your First Android Application
Understanding The Anatomy Of An Android Application
Defining Your Application Using The Android Manifest File
Managing Application Resources
Exploring User Interface Screen Elements
Designing User Interfaces With Layouts
Drawing And Working With Animation
Using Android Data And Storage Apis
Sharing Data Between Applications With Content Providers
Using Android Networking Apis
Using Android Web Apis
Using Location-based Services (lbs) Apis
Using Android Multimedia Apis
Using Android Telephony Apis
Using Android 3d Graphics With Opengl Es
Using The Android Ndk
Using Android’s Optional Hardware Apis
Working With Notifications
Working With Services
Extending Android Application Reach
Managing User Accounts And Synchronizing User Data
Handling Advanced User Input
Targeting Different Device Configurations And Languages
The Mobile Software Development Process
Designing And Developing Bulletproof Android Applications
Testing Android Applications
Selling Your Android Application
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.