Defining UI State

For a little bit of polish, pre-populate the search text box with the saved query when the user presses the search icon to expand the search view.

Unfortunately, there is not a great way for you to access your stored query at the same time that you initialize your search view. Since DataStore exposes its data asynchronously, there is no good way to access its value during the creation of a fragment. In Chapter 12, you read from your on-disk databases asynchronously to avoid blocking the main thread. Because DataStore is backed by on-disk storage, these same performance concerns apply – and mean that you need coroutines once more.

You have already seen how to send asynchronous values to your UI by leveraging StateFlow ...

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.