I have to agree with Rick this chapter needs help - as someone who has written CompSci books I know how hard it is especially near the end to make each chapter as perfect as the ones the author championed earlier in the writing process I find the behavior of the sample code very strange, especially when I hit the search button, enter “dogs”, and then hit the virtual keyboard “search button” to tell it to perform the search. First the background becomes Brian’s image, then they are replaced with the items of the original preceding search which was already there, then when the search data comes in the screen is again replaced with the photos matching the search. A very unsettling experience for the user (on a Galaxy S4). Maybe much of this is due to network lag and the fact that my LRU cache from the previous chapter challenge is not integrated but it sure seems weird to see the Brian matrix, then a couple tils of the last search, then results of the new search coming in.
Also the search text on the action bar stays in place after the search - I think after the search the UI should return to “non-search” mode and display the search and X icons again. No idea how to do this - a suggestion would be appreciated.
As for the toast challenge the difficulty was not in grabbing the “total” field from the XML returned from Flickr, but in getting it to the main thread for the toast. I used the PreferencesManager and Shared Preferences to store it in the AsyncTask, but this also required me to pass the Activity Context to the AsyncTask routines fetchItems() and search() since I could not figure out a way to get access to the main thread context to use the PreferencesManager!
In the onPostExecute() method back on the main thread I grabbed the total stored by the AsyncTask, easily toasted it, and also cleared the PM key back to null.
What other manner of IPC might have been better for sending the total back to requesting task without redoing the interface? Obviously instead of just returning the ArrayAdapter it would be best to return an encapsulating object that contains both the ArrayAdapter AND any other ancillary info about the array contents as the Flickr API does return in the top level record requested. But are there other IPC that would be easy as what I did with the PM Shared Preferences?