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 String
s, and the values can be String
s or primitive-type values.
This app reads the saved searches in the Activity
’s onCreate
method—this is acceptable only because the amount of data being loaded is small. When an app is launched, Android creates a main thread called the UI thread which handles all of the GUI interactions. All GUI processing must be performed in this thread. Extensive input/output operations, such as loading data from files and databases should not be performed on the UI thread, because such operations can affect your app’s responsiveness. We’ll show how to perform I/O in separate threads in later chapters.