Component - ViewModel creation
This UI component shows how a ViewModel is created and used in an Android app with Kotlin. The ViewModel holds data that survives screen rotations and helps keep UI code clean and simple.
This UI component shows how a ViewModel is created and used in an Android app with Kotlin. The ViewModel holds data that survives screen rotations and helps keep UI code clean and simple.
Activity
└── ViewModelProvider
└── ViewModel
└── LiveData
└── Observer
└── UI Components (TextView, Button)ViewModel in Android development?counter.value is printed after increment() is called twice?
class CounterViewModel : ViewModel() {
val counter = MutableLiveData(0)
fun increment() {
counter.value = (counter.value ?: 0) + 1
}
}class SampleViewModel : ViewModel() {
val data = MutableLiveData<String>()
fun updateData(newData: String) {
data.value = newData
}
fun getData(): String {
return data.value
}
}Pass the test and this pattern moves unknown