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,