Chapter 6. Files, Saving State, and Preferences

WHAT'S IN THIS CHAPTER?

  • Persisting simple application data

  • Saving Activity instance data between sessions

  • Creating Preference Screens and managing application preferences

  • Saving and loading files and managing the local file system

  • Including static files as external resources

In this chapter you'll be introduced to two of the simplest but most versatile data persistence techniques in Android — Shared Preferences and local files.

Saving and loading data are essential for most applications. At a minimum, an Activity should save its user interface (UI) state each time it moves into the background. This ensures that the same UI state is presented when the Activity returns to the foreground, even if the process has been killed and restarted before that happens.

It's also likely that you'll need to save user application preferences and UI selections or data entry. Android's nondeterministic Activity and application lifetimes make persisting UI state and application data between sessions particularly important. Android offers several alternatives for saving application data, each optimized to fulfill a particular need.

Shared Preferences are a simple, lightweight key/value pair mechanism for saving primitive application data, most commonly a user's application preferences. Android also provides access to the local file system, through both specialized methods and the normal Java.IO classes.

SAVING SIMPLE APPLICATION DATA

The data-persistence techniques ...

Get Professional Android™ 2 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.