Saving Simple Application Data in Android



There are two lightweight techniques for saving simple application data for Android applications —


Shared Preferences and a pair of event handlers used for saving Activity instance details. Both mechanisms use a name/value pair (NVP) mechanism to store simple primitive values.


Using SharedPreferences, you can create named maps of key/value pairs within your application that can be shared between application components running in the same Context. Shared Preferences support the primitive types Boolean, string, fl oat, long, and integer, making them an ideal way to quickly store default values, class instance variables, the current UI state, and user preferences. They are most commonly used to persist data across user sessions and to share settings between application components.


Alternatively, Activities offer the onSaveInstanceState handler. It’s designed specifi cally to persist the UI state when the Activity becomes eligible for termination by a resource-hungry run time. The handler works like the Shared Preference mechanism. It offers a Bundle parameter that represents a key/value map of primitive types that can be used to save the Activity’s instance values. This Bundle is then made available as a parameter passed in to the onCreate and onRestoreInstanceState method handlers.


This UI state Bundle is used to record the values needed for an Activity to provide an identical UI following unexpected restarts.

Editor: ankita Added on: 2013-03-01 13:10:22 Total View:353







Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---