Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my custom objects Parcelable?

I'm trying to make my objects Parcelable. However, I have custom objects and those objects have ArrayList attributes of other custom objects I have made.

What would be the best way to do this?

like image 747
Jean Jimenez Avatar asked Aug 24 '11 20:08

Jean Jimenez


People also ask

How do you make an object Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

How do you add a Parcelable implementation?

You can install this plugin by going to Android Studio -> File -> Settings -> Plugins -> Browse repositories : Here are all the Java types it supports: Types implementing Parcelable. Custom support (avoids Serializable / Parcelable implementation) for: Date , Bundle.

What does Parcelable mean?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

Why Parcelable is faster than serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.


1 Answers

You can find some examples of this here, here (code is taken here), and here.

You can create a POJO class for this, but you need to add some extra code to make it Parcelable. Have a look at the implementation.

public class Student implements Parcelable{         private String id;         private String name;         private String grade;          // Constructor         public Student(String id, String name, String grade){             this.id = id;             this.name = name;             this.grade = grade;        }        // Getter and setter methods        .........        .........         // Parcelling part        public Student(Parcel in){            String[] data = new String[3];             in.readStringArray(data);            // the order needs to be the same as in writeToParcel() method            this.id = data[0];            this.name = data[1];            this.grade = data[2];        }         @Оverride        public int describeContents(){            return 0;        }         @Override        public void writeToParcel(Parcel dest, int flags) {            dest.writeStringArray(new String[] {this.id,                                                this.name,                                                this.grade});        }        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {            public Student createFromParcel(Parcel in) {                return new Student(in);             }             public Student[] newArray(int size) {                return new Student[size];            }        };    } 

Once you have created this class, you can easily pass objects of this class through the Intent like this, and recover this object in the target activity.

intent.putExtra("student", new Student("1","Mike","6")); 

Here, the student is the key which you would require to unparcel the data from the bundle.

Bundle data = getIntent().getExtras(); Student student = (Student) data.getParcelable("student"); 

This example shows only String types. But, you can parcel any kind of data you want. Try it out.

EDIT: Another example, suggested by Rukmal Dias.

like image 194
Kiril Kirilov Avatar answered Sep 23 '22 17:09

Kiril Kirilov