Sobes.tech
Junior — Middle

What methods can be used to implement information exchange between two interface components or screens of an application?

sobes.tech AI

Answer from AI

Communication between two interface components or screens in Android can be implemented in several ways:

  1. Intent with data transfer — when transitioning from one Activity to another, data can be passed via Intent using putExtra methods and retrieved in the target Activity through getIntent().getExtras().

  2. Shared ViewModel — when using MVVM architecture and fragments, a shared ViewModel can be created to store state and data accessible to both fragments.

  3. Callback interfaces — one component implements an interface, and the other calls methods of this interface to transfer data.

  4. EventBus or LiveData — using libraries like EventBus or LiveData, you can subscribe to events and pass data asynchronously.

  5. SharedPreferences or database — for more persistent storage and data exchange.

Example of data transfer via Intent:

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)

// In SecondActivity
val value = intent.getStringExtra("key")