How can I make my custom objects Parcelable?

后端 未结 11 2549
旧巷少年郎
旧巷少年郎 2020-11-21 07:40

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.

11条回答
  •  伪装坚强ぢ
    2020-11-21 07:58

    Android parcable has some unique things. Those are given bellow:

    1. You have to read Parcel as the same order where you put data on parcel.
    2. Parcel will empty after read from parcel. That is if you have 3 data on your parcel. Then after read 3 times parcel will be empty.

    Example: To make a class Parceble it must be implement Parceble. Percable has 2 method:

    int describeContents();
    void writeToParcel(Parcel var1, int var2);
    

    Suppose you have a Person class and it has 3 field, firstName,lastName and age. After implementing Parceble interface. this interface is given bellow:

    import android.os.Parcel;
    import android.os.Parcelable;
    public class Person implements Parcelable{
        private String firstName;
        private String lastName;
        private int age;
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeString(firstName);
            parcel.writeString(lastName);
            parcel.writeInt(age);
        }
    
    }
    

    Here writeToParcel method we are writing/adding data on Parcel in an order. After this we have to add bellow code for reading data from parcel:

    protected Person(Parcel in) {
            firstName = in.readString();
            lastName = in.readString();
            age = in.readInt();
        }
    
        public static final Creator CREATOR = new Creator() {
            @Override
            public Person createFromParcel(Parcel in) {
                return new Person(in);
            }
    
            @Override
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
    

    Here, Person class is taking a parcel and getting data in same an order during writing.

    Now during intent getExtra and putExtra code is given bellow:

    Put in Extra:

    Person person=new Person();
                    person.setFirstName("First");
                    person.setLastName("Name");
                    person.setAge(30);
    
                    Intent intent = new Intent(getApplicationContext(), SECOND_ACTIVITY.class);
                    intent.putExtra()
                    startActivity(intent); 
    

    Get Extra:

    Person person=getIntent().getParcelableExtra("person");
    

    Full Person class is given bellow:

    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Person implements Parcelable{
        private String firstName;
        private String lastName;
        private int age;
    
    
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeString(firstName);
            parcel.writeString(lastName);
            parcel.writeInt(age);
        }
    
        protected Person(Parcel in) {
            firstName = in.readString();
            lastName = in.readString();
            age = in.readInt();
        }
    
        public static final Creator CREATOR = new Creator() {
            @Override
            public Person createFromParcel(Parcel in) {
                return new Person(in);
            }
    
            @Override
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
    
    }
    
    Hope this will help you 
    Thanks :)
    

提交回复
热议问题