Sobes.tech
Junior — Middle

How is data flow implemented between the data source and the user interface in the system?

sobes.tech AI

Answer from AI

In Android, the data flow between the source and the UI is usually implemented using architectural components such as LiveData, Flow, or RxJava.

For example, a ViewModel contains a data source (such as LiveData), which is observed in an Activity or Fragment. When the data changes, LiveData notifies the UI, and it updates automatically.

Example with LiveData:

class MyViewModel : ViewModel() {
    private val _data = MutableLiveData<String>()
    val data: LiveData<String> = _data

    fun loadData() {
        // load data from repository
        _data.value = "New data"
    }
}

// In Activity or Fragment
viewModel.data.observe(this) { newData ->
    textView.text = newData
}

This approach ensures reactive UI updates when data changes.