androidengineers.Book a session
โ† All interview questions
DatabaseIntermediate2 min

A refresh fails and an offline-capable screen goes blank. How would you model its state?

Answer

A single mutually exclusive Loading, Success, or Error value can be too coarse. The screen may simultaneously have useful saved lessons and a failed attempt to refresh them.

Example

A possible screen model, with an existing Lesson domain type:

data class LibraryState(
    val lessons: List<Lesson> = emptyList(),
    val hasLoaded: Boolean = false,
    val isRefreshing: Boolean = false,
    val refreshError: String? = null
)

Observe persisted lessons as the content source. Refresh writes successful results into storage. If it fails, retain existing content and show a retry affordance. A first-load failure with no content needs a different presentation from a refresh failure with 30 readable lessons.

hasLoaded distinguishes an uninitialized list from a successful empty result. In a production model, a typed error can carry recovery information while the UI selects localized messages.

Trade-off

Cached content is not always safe to act on. Showing a saved lesson title is different from trusting a stale entitlement or checkout price. Define which actions require revalidation and communicate unavailable actions explicitly.

Follow-up to practise

What should a successful empty refresh do? If the API response is authoritative, update the relevant stored result set to empty. Preserving stale content on failures must not become a rule that ignores legitimate deletions.

Reference

Android Developers: Network and database as a paging source

Mark this when you can explain the answer in your own words.

Share & Help Others

Help fellow developers prepare for interviews

Sharing helps the Android community grow ๐Ÿ’š

Keep practising