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




Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.