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:
-
Intent with data transfer — when transitioning from one Activity to another, data can be passed via
IntentusingputExtramethods and retrieved in the target Activity throughgetIntent().getExtras(). -
Shared ViewModel — when using MVVM architecture and fragments, a shared ViewModel can be created to store state and data accessible to both fragments.
-
Callback interfaces — one component implements an interface, and the other calls methods of this interface to transfer data.
-
EventBus or LiveData — using libraries like EventBus or LiveData, you can subscribe to events and pass data asynchronously.
-
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")