MVVM (Model-View-ViewModel) is the most common architecture pattern in modern Android apps. It separates concerns into three layers:

ViewActivity / Fragment ViewModelholds UI state RepositoryRoom + Retrofit observes LiveDatauser actions requests datareturns Flow/LiveData

  • View (Activity/Fragment) — displays UI and forwards user actions; contains no business logic.
  • ViewModel — holds UI state and exposes it as observable data; survives configuration changes.
  • Repository (part of the Model layer) — the single source of truth for data, abstracting away whether it comes from a local database (Room) or the network (Retrofit).
class UserRepository(private val api: ApiService, private val dao: UserDao) {
    suspend fun getUser(id: Int): User {
        val cached = dao.getUserById(id)
        if (cached != null) return cached

        val fresh = api.fetchUser(id)
        dao.insertUser(fresh)
        return fresh
    }
}

Notice the ViewModel never talks to Retrofit or Room directly — it only knows about the Repository. This makes the ViewModel easy to test (you can pass in a fake Repository) and means the data source can change (add caching, switch APIs) without touching the UI layer at all.