Component - MVVM pattern
The MVVM pattern helps organize Android apps by separating the user interface, data handling, and business logic into three parts: Model, View, and ViewModel. This makes apps easier to build, test, and maintain.
The MVVM pattern helps organize Android apps by separating the user interface, data handling, and business logic into three parts: Model, View, and ViewModel. This makes apps easier to build, test, and maintain.
Activity (View)
└── ViewModel
└── ModelViewModel in the MVVM pattern?increment() is called twice?class CounterViewModel : ViewModel() {
private val _count = MutableLiveData(0)
val count: LiveData<Int> = _count
fun increment() {
_count.value = (_count.value ?: 0) + 1
}
}class UserViewModel : ViewModel() {
val userName = MutableLiveData<String>()
fun updateName(newName: String) {
userName.value = newName
}
fun getName(): String {
return userName.value!!
}
}1. Repository fetches data asynchronously 2. ViewModel exposes LiveData for user data 3. View observes LiveData and updates UI Which code snippet fits this pattern?
Pass the test and this pattern moves unknown