Junior — Middle
How is data transferred from business logic to the user interface?
sobes.tech AI
Answer from AI
In Android, data transfer from business logic to the user interface is usually done through ViewModel and LiveData (or StateFlow/Flow in modern approaches).
The main pattern is MVVM (Model-View-ViewModel):
- Business logic resides in the Model layer or in repositories.
- ViewModel receives data from business logic and stores it in LiveData or other observable objects.
- UI (Activity/Fragment) subscribes to these data and updates when they change.
Example with LiveData:
class MyViewModel : ViewModel() {
val data: MutableLiveData<String> = MutableLiveData()
fun loadData() {
// Get data from business logic
data.value = "Hello, world!"
}
}
// In Activity or Fragment
viewModel.data.observe(this) { value ->
textView.text = value
}
This approach ensures reactive UI updates when data changes and separates business logic from the presentation.