I'm used to develop in ios where I never need to make a model parcelable so the concept is not very clear to me. I have a class "game" like:
//removed the method to make it more readable.
public class Game implements Parcelable {
private int _id;
private ArrayList<Quest> _questList;
private int _numberOfGames;
private String _name;
private Date _startTime;
public Game(String name, ArrayList<Quest> quests, int id){
_name = name;
_questList = quests;
_numberOfGames = quests.size();
_id = id;
}
}
I want to start an activity and pass the game object to the activity with my intent, but it turned out you can't pass custom objects by default but they need to be parcelable. So I've added:
public static final Parcelable.Creator<Game> CREATOR
= new Parcelable.Creator<Game>() {
public Game createFromParcel(Parcel in) {
return new Game(in);
}
public Game[] newArray(int size) {
return new Game[size];
}
};
private Game(Parcel in) {
_id = in.readInt();
_questList = (ArrayList<Quest>) in.readSerializable();
_numberOfGames = in.readInt();
_name = in.readString();
_startTime = new Date(in.readLong());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(_id);
out.writeSerializable(_questList);
out.writeInt(_numberOfGames);
out.writeString(_name);
out.writeLong(_startTime.getTime());
}
But now I get the warning that the custom arraylist _questList is not parcelable Game.
Quest is an abstract class so it cant implement.
public static final Parcelable.Creator<Game> CREATOR = new Parcelable.Creator<Game>() {
public Game createFromParcel(Parcel source) {
return new Game(source);
}
public Game[] newArray(int size) {
return new Game[size];
}
};
So my question is: When do I need to implement parcelable, do I have to add it to every custom object I want to pass (even if in another custom object)? I can't imagine they don't have something easier for android to pass a custom object with an array list of custom objects.