Wednesday, 12 April 2017

How to Implement Parcelable in Android with String Integer and ArrayLists as fields

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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1. public class School implements Parcelable {
  2. private String name;
  3. private ArrayList<Student> studentsList = new ArrayList<Student>();
  4.  
  5. public School(){ 
  6.  
  7. } 
  8.  
  9. public String getName() {
  10. return name;
  11. } 
  12.  
  13. public void setName(String name) {
  14. this.name = name;
  15. } 
  16.  
  17. public ArrayList<Student> getStudentsList() {
  18. return studentsList;
  19. } 
  20.  
  21. public void setStudentsList(ArrayList<Student> studentsList) {
  22. this.studentsList = studentsList;
  23. } 
  24.  
  25. protected School(Parcel in) {
  26. name = in.readString();
  27. if (in.readByte() == 0x01) {
  28. studentsList = new ArrayList<Student>();
  29. in.readList(studentsList, Student.class.getClassLoader());
  30. } else { 
  31. studentsList = null;
  32. } 
  33. } 
  34.  
  35. @Override 
  36. public int describeContents() { 
  37. return 0; 
  38. } 
  39.  
  40. @Override 
  41. public void writeToParcel(Parcel dest, int flags) {
  42. dest.writeString(name);
  43. if (studentsList == null) {
  44. dest.writeByte((byte) (0x00));
  45. } else { 
  46. dest.writeByte((byte) (0x01));
  47. dest.writeList(studentsList);
  48. } 
  49. } 
  50.  
  51. @SuppressWarnings("unused") 
  52. public static final Parcelable.Creator<School> CREATOR = new Parcelable.Creator<School>() {
  53. @Override 
  54. public School createFromParcel(Parcel in) { 
  55. return new School(in); 
  56. } 
  57.  
  58. @Override 
  59. public School[] newArray(int size) { 
  60. return new School[size]; 
  61. } 
  62. }; 
  63. } 
  64. /////////////////////////////////////////////////////////////////////////////////////

Now create Student class
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Student.java
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  1. public
    class Student implements Parcelable {
  2. private String name;
  3. private int age;
  4. private float height;
  5.  
  6. protected Student(Parcel in) {
  7. name = in.readString();
  8. age = in.readInt();
  9. height = in.readFloat();
  10. } 
  11.  
  12. public Student(){ 
  13.  
  14. } 
  15.  
  16.  
  17. public String getName() {
  18. return name;
  19. } 
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. } 
  24.  
  25. public int getAge() { 
  26. return age;
  27. } 
  28.  
  29. public void setAge(int age) {
  30. this.age = age;
  31. } 
  32.  
  33. public float getHeight() { 
  34. return height;
  35. } 
  36.  
  37. public void setHeight(float height) {
  38. this.height = height;
  39. } 
  40.  
  41. @Override 
  42. public int describeContents() { 
  43. return 0; 
  44. } 
  45.  
  46. @Override 
  47. public void writeToParcel(Parcel dest, int flags) {
  48. dest.writeString(name);
  49. dest.writeInt(age);
  50. dest.writeFloat(height);
  51. } 
  52.  
  53. @SuppressWarnings("unused") 
  54. public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
  55. @Override 
  56. public Student createFromParcel(Parcel in) { 
  57. return new Student(in); 
  58. } 
  59.  
  60. @Override 
  61. public Student[] newArray(int size) { 
  62. return new Student[size]; 
  63. } 
  64. }; 
  65. } 
  66. /////////////////////////////////////////////////////////////////////////////////////////


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
//////////////////////////////////////////////////////////////////////////////////////////
  1. School school = new School();
  2. school.setName("Jnana Bharathi");
  3. Student student1 = new Student();
  4. student1.setName("Arun");
  5. student1.setAge(9);
  6. student1.setHeight(4.1f);
  7. Student student2 = new Student();
  8. student2.setName("Tarun");
  9. student2.setAge(10);
  10. student2.setHeight(3.9f);
  11. Student student3 = new Student();
  12. student3.setName("Varun");
  13. student3.setAge(11);
  14. student3.setHeight(4.0f);
  15. ArrayList<Student> studentArrayList = new ArrayList<>();
  16. studentArrayList.add(student1);
  17. studentArrayList.add(student2);
  18. studentArrayList.add(student3);
  19. school.setStudentsList(studentArrayList);
  20. /* call second activity using intent and send School instance */
  21. Intent intent = new Intent(MainActivity.this,SecondActivity.class);
  22. intent.putExtra("school_object",school);
  23. startActivity(intent);
  24. ////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Second activity
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1. School school = (School)getIntent().getParcelableExtra("school_object");
  2. Log.d("school=",school.getName());
  3. for (int index=0;index<school.getStudentsList().size();index++){
  4. Student student = school.getStudentsList().get(index);
  5. Log.d("**************","");
  6. Log.d("student name =",student.getName());
  7. Log.d("student age = ",student.getAge()+"");
  8. Log.d("student height =",student.getHeight()+"");
  9. Log.d("**************","");
  10. } 
  11. ///////////////////////////////////////////////////////////////////////////////////////
  12. You can download sample from github, below i have given link

Please leave suggestions if you face any problems.