Providing User Control over Polling

Some users may not want your app to run in the background. An important control to provide users is the ability to enable and disable background polling.

For PhotoGallery, you will add a menu item to the toolbar that will toggle your worker when selected. You will also update your work request to run your worker periodically instead of just once.

To toggle your worker, you first need to determine whether the worker is currently running. To do this, supplement your QueryPreferences to store a flag indicating if the worker is enabled.

Listing 27.14  Saving Worker state (QueryPreferences.kt)

private const val PREF_SEARCH_QUERY = "searchQuery" private const val PREF_LAST_RESULT_ID = "lastResultId" ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.