Sobes.tech
Junior — Middle

How can data be exchanged between two windows in an Android application?

sobes.tech AI

Answer from AI

In Android, data exchange between two windows (Activities) is usually done using an Intent with additional data (Extras). When launching the second window, data can be passed via Intent, and in the second window, it can be retrieved from the Intent. You can also use startActivityForResult and onActivityResult to get a result from the second window.

Example of data transfer:

// In the first Activity
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key", "value");
startActivity(intent);

// In the second Activity
String value = getIntent().getStringExtra("key");

For data exchange between fragments within the same Activity, you can use ViewModel or callback interfaces.