Pass Data from one activity to other in Android

To pass String, boolean, float, double, int data types to other activity

intent.putExtra("KEY", VALUE);

Example

Intent intent = new Intent(this, MapsActivity.class);

intent.putExtra("LONGITUDE", Double.parseDouble(longitude));

intent.putExtra("LONGITUDE", Double.parseDouble(longitude));

intent.putExtra("isLocation", true);

startActivity(intent);

To get the passed values from 2nd activity

Intent intent = getIntent();

if(intent!=null) {

double myLat = intent.getDoubleExtra("LATITUDE", 0);

double myLong = intent.getDoubleExtra("LONGITUDE", 0);

boolean isLocation = intent.getBooleanExtra("isLocation", false);

}

To pass objects to other activity

To pass the objects to other activity, object class should be of Serializable

Example

public class CustomerDetails implements Serializable {

String name;

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

Intent intent = new Intent(this, Customer.class);

CustomerDetails customerDetails = new CustomerDetails();

customerDetails.setName("Krishna");

intent.putExtra("CustomerDetails", customerDetails);

startActivity(intent);

To get the passed object from 2nd activity

Intent intent = getIntent();

if(intent!=null) {

CustomerDetails customerDetails = (CustomerDetails) intent.getSerializableExtra("CustomerDetails");

}