Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 27  Search


Simple Persistence with Shared Preferences


The last piece of functionality you need to add is to actually use the query entered in the SearchView
when the search request is submitted.


In your app, there will only be one active query at a time. That query should be persisted (remembered
by the app) between restarts of the app (even after the user turns off the device). You will achieve this
by writing the query string to shared preferences. Any time the user submits a query, you will first
write the query to shared preferences, overwriting whatever query was there before. When a search is
executed against Flickr, you will pull the query string from shared preferences and use it as the value
for the text parameter.


Shared preferences are files on your filesystem that you read and edit using the SharedPreferences
class. An instance of SharedPreferences acts like a key-value store, much like Bundle, except that it
is backed by persistent storage. The keys are strings, and the values are atomic data types. If you look
at them you will see that the files are simple XML, but SharedPreferences makes it easy to ignore
that implementation detail. Shared preferences files are stored in your application’s sandbox, so you
should not store sensitive information (like passwords) there.


To get a specific instance of SharedPreferences, you can use the
Context.getSharedPreferences(String, int) method. However, in practice, you will often not care
too much about the specific instance, just that it is shared across the entire app. In that case, it is better
to use the PreferenceManager.getDefaultSharedPreferences(Context) method, which returns an
instance with a default name and private permissions (so that the preferences are only available from
within your application).


Add a new class named QueryPreferences, which will serve as a convenient interface for reading and
writing the query to and from shared preferences.


Listing 27.11  Adding class to manage stored query (QueryPreferences.java)


public class QueryPreferences {
private static final String PREF_SEARCH_QUERY = "searchQuery";


public static String getStoredQuery(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(PREF_SEARCH_QUERY, null);
}


public static void setStoredQuery(Context context, String query) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(PREF_SEARCH_QUERY, query)
.apply();
}
}


PREF_SEARCH_QUERY is used as the key for the query preference. You will use this key any time you
read or write the query value.


The getStoredQuery(Context) method returns the query value stored in shared preferences. It does so
by first acquiring the default SharedPreferences for the given context. (Because QueryPreferences
does not have a Context of its own, the calling component will have to pass its context as input.)

Free download pdf