Sobes.tech
Junior — Middle

How is data exchanged between two screens in an Android application?

sobes.tech AI

Answer from AI

Data exchange between two screens (Activities) in Android is usually done using Intent. When launching the second screen, additional data can be put into the Intent using the putExtra() method. In the second screen, this data is retrieved via getIntent().getExtras().

Example of passing a string from the first Activity to the second:

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

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

You can also get a result from the second screen using startActivityForResult() and handle the data in onActivityResult(), or use modern APIs with ActivityResultLauncher.