Sobes.tech
Junior — Middle

How can you notify the View about events or changes in the MVP architecture?

sobes.tech AI

Answer from AI

In MVP (Model-View-Presenter) architecture, the View is notified of events or changes through an interface that the View implements and that calls the Presenter. The main notification methods are:

  • Callbacks: The Presenter calls methods of the View interface to inform about changes.
  • Observer: The View subscribes to Presenter events and receives notifications.
  • Using LiveData or RxJava (in Android): The Presenter updates data, and the View subscribes to changes and reacts automatically.

Example with a View interface:

interface MyView {
    fun showData(data: String)
    fun showError(message: String)
}

class MyPresenter(private val view: MyView) {
    fun loadData() {
        // load data
        val data = "Sample data"
        view.showData(data) // notify the View
    }
}

Thus, the Presenter controls the logic and notifies the View through interface methods.