Search code examples
androidclassparcelableparcel

Implementing Parcelable interface in a class that is composed of other objects


I have the following code to create a basic profile within my app, it contains an object of another class which holds all of the user's performance data within arrays and various other primitive data types. How would I implement the parcelable interface within this class so that I can pass the user profile object to various activities within my app either for reading or writing? Would I have to implement the parcelable interface within the Performance class as well as this Profile class?

public class Profile {

private String name;
private String email;
private Performance Progress;

private int seed;
private int LastRevisionQuestion = 0;

private int[] LastQuestionQuizLinear = {0,0,0};
private int[] tempScoreQuizLinear = {0,0,0};

private int[] LastQuestionQuizRandom = {0,0,0};
private int[] tempScoreQuizRandom = {0,0,0};

private int[] LastQuestionTimedQuizLinear = {0,0,0};
private int[] tempScoreTimedQuizLinear = {0,0,0};
private int[] tempTotalTimeTimedQuizLinear = {0,0,0};

private int[] LastQuestionTimedQuizRandom = {0,0,0};
private int[] tempScoreTimedQuizRandom = {0,0,0};
private int[] tempTotalTimeTimedQuizRandom = {0,0,0};


/**
 * Default constructor
 */
public Profile(){
    this("ProfileName", "ProfileEmail");
}

/**
 * Overloaded constructor takes user name and email address as input parameter to create a new user
 * @param name
 * @param email
 */
public Profile(String name, String email){
    this.name = name;
    this.email = email;
    setProgress(new Performance());
}
}

Solution

  • You can only parcel simple data types (int, String, boolean, etc), parcelables, and maps. So yes, your best bet will be to implement parcelable in Performance as well.