14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6: Data Storage, Retrieval, and Sharing<br />

160<br />

Content Providers offer a generic interface to any data source. They effectively decouple the underlying<br />

data storage technique from the application layer.<br />

By default, access to all fi les, databases, and preferences is restricted to the application that created<br />

them. Content Providers offer a managed way for your applications to share private data with other<br />

applications. As a result, your applications can use the Content Providers offered by others, including<br />

native providers.<br />

Android Techniques for Saving Data<br />

The data persistence techniques in Android provide options for balancing speed, effi ciency, and<br />

robustness:<br />

❑ Shared Preferences When storing the UI state, user preferences, or application settings, you<br />

want a lightweight mechanism to store a known set of values. Shared Preferences let you save<br />

groups of key/value pairs of primitive data as named preferences.<br />

❑ Files It’s not pretty, but sometimes writing to, and reading from, fi les directly is the only way<br />

to go. Android lets you create and load fi les on the device’s internal or external media.<br />

❑ SQLite Databases When managed, structured data is the best approach, Android offers the<br />

SQLite relational database library. Every application can create its own databases over which it<br />

has total control.<br />

❑ Content Providers Rather than a storage mechanism in their own right, Content Providers let<br />

you expose a well-defi ned interface for using and sharing private data. You can control access<br />

to Content Providers using the standard permission system.<br />

Saving Simple <strong>Application</strong> Data<br />

There are two lightweight techniques for saving simple application data for Android applications —<br />

Shared Preferences and a pair of event handlers used for saving Activity instance details. Both mechanisms<br />

use a name/value pair (NVP) mechanism to store simple primitive values.<br />

Using SharedPreferences, you can create named maps of key/value pairs within your application<br />

that can be shared between application components running in the same Context.<br />

Shared Preferences support the primitive types Boolean, string, fl oat, long, and integer, making them<br />

an ideal way to quickly store default values, class instance variables, the current UI state, and user<br />

preferences. They are most commonly used to persist data across user sessions and to share settings<br />

between application components.<br />

Alternatively, Activities offer the onSaveInstanceState handler. It’s designed specifi cally to persist<br />

the UI state when the Activity becomes eligible for termination by a resource-hungry run time.<br />

The handler works like the Shared Preference mechanism. It offers a Bundle parameter that represents<br />

a key/value map of primitive types that can be used to save the Activity’s instance values. This Bundle

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!