How to Implement Parcelable in Android with String, Integer and ArrayLists as fields
Parcelable and Serialization are used for marshaling and unmarshaling Java objects.
Parcelable :
Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
Serializable :
Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.
Sample
Lets take an example of School class which has a name and obiviously School has list of Students.
First lets create School class which implements parcelable.
Remember four points to implement Parcelable
1. Creare Protected Constructor with Parcel as argument.
2. Override describeContents which always returns zero.
3. Override writeToParcel method with Parcel and int as arguments.
4. Create instance of Creator interface.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
School.java
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class School implements Parcelable {
- private String name;
- private ArrayList<Student> studentsList = new ArrayList<Student>();
- public School(){
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public ArrayList<Student> getStudentsList() {
- return studentsList;
- }
- public void setStudentsList(ArrayList<Student> studentsList) {
- this.studentsList = studentsList;
- }
- protected School(Parcel in) {
- name = in.readString();
- if (in.readByte() == 0x01) {
- studentsList = new ArrayList<Student>();
- in.readList(studentsList, Student.class.getClassLoader());
- } else {
- studentsList = null;
- }
- }
- @Override
- public int describeContents() {
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(name);
- if (studentsList == null) {
- dest.writeByte((byte) (0x00));
- } else {
- dest.writeByte((byte) (0x01));
- dest.writeList(studentsList);
- }
- }
- @SuppressWarnings("unused")
- public static final Parcelable.Creator<School> CREATOR = new Parcelable.Creator<School>() {
- @Override
- public School createFromParcel(Parcel in) {
- return new School(in);
- }
- @Override
- public School[] newArray(int size) {
- return new School[size];
- }
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////
Now create Student class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Student.java
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Student implements Parcelable {- private String name;
- private int age;
- private float height;
- protected Student(Parcel in) {
- name = in.readString();
- age = in.readInt();
- height = in.readFloat();
- }
- public Student(){
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public float getHeight() {
- return height;
- }
- public void setHeight(float height) {
- this.height = height;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(name);
- dest.writeInt(age);
- dest.writeFloat(height);
- }
- @SuppressWarnings("unused")
- public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
- @Override
- public Student createFromParcel(Parcel in) {
- return new Student(in);
- }
- @Override
- public Student[] newArray(int size) {
- return new Student[size];
- }
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////////
Now we want to send Student object from one activity to another activity
In one activity create instance of School and Students classes.
/////////////////////////////////////////////////////////////////////////////////////////
First activity
//////////////////////////////////////////////////////////////////////////////////////////
- School school = new School();
- school.setName("Jnana Bharathi");
- Student student1 = new Student();
- student1.setName("Arun");
- student1.setAge(9);
- student1.setHeight(4.1f);
- Student student2 = new Student();
- student2.setName("Tarun");
- student2.setAge(10);
- student2.setHeight(3.9f);
- Student student3 = new Student();
- student3.setName("Varun");
- student3.setAge(11);
- student3.setHeight(4.0f);
- ArrayList<Student> studentArrayList = new ArrayList<>();
- studentArrayList.add(student1);
- studentArrayList.add(student2);
- studentArrayList.add(student3);
- school.setStudentsList(studentArrayList);
- /* call second activity using intent and send School instance */
- Intent intent = new Intent(MainActivity.this,SecondActivity.class);
- intent.putExtra("school_object",school);
- startActivity(intent);
- ////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Second activity
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- School school = (School)getIntent().getParcelableExtra("school_object");
- Log.d("school=",school.getName());
- for (int index=0;index<school.getStudentsList().size();index++){
- Student student = school.getStudentsList().get(index);
- Log.d("**************","");
- Log.d("student name =",student.getName());
- Log.d("student age = ",student.getAge()+"");
- Log.d("student height =",student.getHeight()+"");
- Log.d("**************","");
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- You can download sample from github, below i have given link
Please leave suggestions if you face any problems.