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 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.