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

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap