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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Now create Student class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Student.java
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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.