How can I pass an object of a custom type from one Activity to another using the
putExtra()
method of the class Intent?
implement serializable in your class
public class Place implements Serializable{
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then you can pass this object in intent
Intent intent = new Intent(this, SecondAct.class);
intent.putExtra("PLACE", Place);
startActivity(intent);
int the second activity you can get data like this
Place place= (Place) getIntent().getSerializableExtra("PLACE");
But when the data become large,this method will be slow.