Like the title says, I don't know why when I try passing a object that contains another object that implements parcelable with writeParcelable
and readParcelable
messed up the data I pass?
by messed up I mean. the ID
is read by Name
, and Name
is not being passed because there is nothing to receive the data, and the object may or may not be passed at all.
it is fine if I simply pass Strings, Integers, Double via parcelable, but not when I try to pass an Object which also implements parcelable.
Maybe I used the wrong method? to my understanding because the object contains another object which implements parcelable, I should use readParcelable and writeParcelable. But if it's wrong can anyone tell me which method I should use and how to use it?
Thank you in advance.
here are the parcelable Objects :
First Object :
public class Absen implements Parcelable{
private String ID;
private String Key;
private String Name;
private int Age;
private Anak Anak;
public Absen(){
//empty constructor
}
public Absen(String ID, String Key, String Name, int Age, Anak Anak)
{
this.ID = ID;
this.Key = Key;
this.Name = Name;
this.Anak = Anak;
this.Age = Age;
}
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
protected Absen (Parcel parcel)
{
this.ID = parcel.readString();
this.Key = parcel.readString();
this.Name = parcel.readString();
this.Anak = parcel.readParcelable(Anak.class.getClassLoader());
this.Age = parcel.readInt();
}
@Override
public void writeToParcel(@NonNull Parcel parcel, int i)
{
parcel.writeString(getID());
parcel.writeString(getKey());
parcel.writeString(getName());
parcel.writeParcelable(getAnak(),i);
parcel.writeInt(getAge());
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Absen> CREATOR = new Creator<Absen>() {
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@Override
public Absen createFromParcel(Parcel parcel) { return new Absen(parcel);}
@Override
public Absen[] newArray(int i) {
return new Absen[i];
}
};
//Setters Getters....
}
Second Object
public class Anak implements Parcelable {
private String ID;
private String Key;
private String Name;
private String parentName;
public Anak{
//empty constructor
}
public Anak(String ID, String Key, String Name, String parentName)
{
this.ID = ID;
this.Key = Key;
this.Name = Name;
this.parentName = parentName;
}
protected Anak (Parcel parcel)
{
this.ID = parcel.readString();
this.Key = parcel.readString();
this.Name = parcel.readString();
this.parentName = parcel.readString();
}
@Override
public void writeToParcel(Parcel parcel, int i)
{
parcel.writeString(getID());
parcel.writeString(getKey());
parcel.writeString(getName());
parcel.writeString(getParentName());
}
@Override
public int describeContents(){ return 0;}
public static final Creator<Anak> CREATOR = new Creator<Anak>() {
@Override
public Anak createFromParcel(Parcel parcel) {
return new Anak(parcel);
}
@Override
public Anak[] newArray(int i) {
return new Anak[i];
}
};
}