Pass data between two Android Activities and access it using Intent

If you have an activity say MainActivity and you want to pass a data to a 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);

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);

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram