4.3.5. SharedPreferences

You can have one or more files containing key–value pairs associated with each app—each key enables you to quickly look up a corresponding value. We use this capability to manipulate a file called searches in which we store the pairs of tags (the keys) and Twitter search queries (the values) that the user creates. To read the key–value pairs from this file we’ll use SharedPreferences objects (package android.content). To modify the file’s contents, we’ll use SharedPreferences.Editor objects (package android.content). The keys in the file must be Strings, and the values can be Strings or primitive-type values.

This app reads the saved searches in the Activity’s onCreate method—this is acceptable only because the amount ...

Get Android™ How to Program, Second Edition 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.