0

I tried to send a parcel from the mainActivity to a fragment and i noticed a strange effect.

When you implement parcelable, android require to implement "writeToParcel" and "describeContents". The strange effect is, if you write something inside "writeToParcel" or nothing, it doesn't matter. Android never call it.

Why these functions are required ?

Here is a working source code :

public class Movie implements Parcelable {
    private String title;
    private Float score;

    public Movie(String title, float score) {
        this.title = title;
        this.score = score;
    }

...... Getter and Setter......

    //Parcelable Methods

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // Don't need to write anything in this area
        /*
        dest.writeString(title);
        dest.writeFloat(score);
        */
    }

    public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
//--------------> You can return null, it's never called and work perfectly!
        public Movie createFromParcel(Parcel in) {
            return null;//new Movie(in);
        }

        public Movie[] newArray(int size) {
            return null;//new Movie[size];
        }
    };
}

Any idear ?

Regards,

Compass
  • 5,867
  • 4
  • 30
  • 42
acs-team
  • 602
  • 6
  • 14
  • It won't do anything if you don't marshall/unmarshall the Parcel. – Compass Jan 02 '15 at 16:08
  • It is used to send data across activities in an efficient way. And you also need to implement the `onSaveInstanceState()` and `onRestoreInstanceState()` that takes return `parcelable` and takes a `parcel` respectively – Biu Jan 02 '15 at 16:18

0 Answers0