0

How do you actually create an object of a class that implements Parcelable? There are lots of examples online that shows exactly what the class should look like, but I can't seem to find something on how to create the object.

If I have a class that implements Parcelable, and the contructor looks like this:

public class ResultForTeam implements Parcelable
{

    String playerA, playerB, teamA, teamB, scores;
    int playerA_games, playerB_games;


    public ResultForTeam(Parcel in) 
    {
        this.playerA = in.readString();
        this.playerB = in.readString();
        this.teamA = in.readString();
        this.teamB = in.readString();
        this.scores = in.readString();
        this.playerA_games = in.readInt();
        this.playerB_games = in.readInt();
    }

how do I then make an object of this class somewhere else in my code? It used to be like this before I implemented Parcelable.

ResultForTeam newResult = new ResultForTeam(playerA, playerB, teamA, teamB, scores, playerA_games, playerB_games);
Code Vader
  • 739
  • 3
  • 9
  • 26

1 Answers1

1

You can keep your old constructor and this overloaded one. You'll need to also implement the describeContents() and writeToParcel() methods. Don't forget to write the static CREATOR field as well. You will then be able to use the class as you always have and use it with a Parcel.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Thanx @Larry! I think I missed somewhere that I had to have 2 constructors for my class. No I can just declare the object like before and pass it around different activities with ArrayList and results = getIntent().getParcelableArrayListExtra("results"); – Code Vader May 16 '15 at 14:52