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 app bar 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 PreferencesRepository to store a flag indicating whether the worker is enabled.
Listing 22.12 Saving Worker
state (PreferencesRepository.kt
)
class PreferencesRepository private constructor( private val dataStore: DataStore<Preferences> ...
Get Android Programming: The Big Nerd Ranch Guide, 5th 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.