Pass data between two Android Activities and access it using Intent

If you have an activity say MainActivity and you want to pass data to the next Activity using Intent you can try the below code snippet.


String name="Joe";
String country="USA";
String city="New York";

Intent mainActivityIntent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("name", name);
intent.putExtra("country", country);
intent.putExtra("city", city);
startActivity(intent);

To use the data back on the send Activity you can make use of the below code,


String name = getIntent().getStringExtra("name");
String country= getIntent().getStringExtra("country");
String city= getIntent().getStringExtra("city");

Syntax: Intent intent= new Intent(getBaseContext(), Target-Activity.class);


This is not an AI-generated article but is demonstrated by a human.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!