Android – Send object from one activity to another Activity using Intent

By -

Here is a simple example about how to pass custom class object from one activity to another activity. To pass an object, your custom class object must be serialized, to do so just implement the Serializable interface.

For example:

import java.io.Serializable;

public class Student implements Serializable
{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
    int rollno;
    String name;

    public int getRollno() {
		return rollno;
	}
	public void setRollno(int rollno) {
		this.rollno = rollno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

Now To pass the object of Student class From Activity ‘A’ to activity ‘B’:

Student stud = new Student();
Intent aActivity = (A.this, B.class);
intent.putExtra("StudentObject", stud);

To retrieve object in second Activity i.e. in ‘B’ activity, write the below line:

Student studObject = getIntent().getSerializableExtra("StudentObject");

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...