I am attempting to learn Android development by making a helper program to a board game I play. I have run into a situation very similar to the one answered in Pass custom object between activities. The difference in my situation is that the custom objects in question are all extending an abstract class.
The abstract class represents a chip (comparable to a card gameplay wise), and is as follows:
import java.util.ArrayList;
public abstract class Chip{
protected String name;
protected int imageID;
protected ArrayList<String> chipColors;
protected ArrayList<String> chipTypes;
public String toString(){
return name;
}
//Getters
public String getName(){ return name; }
public int getImageID() { return imageID; }
public String getSet() { return set; }
//Figure out how I need to deal with colors/types when I get there
}
An example of a class that would extend Chip:
public class ChipA extends Chip{
public ChipA (){
super();
name = "Chip A";
imageID = R.drawable.chipa;
set = "Basic";
chipTypes = new ArrayList<String>();
chipTypes.add("typeA");
chipColors = new ArrayList<String>();
chipColors.add("red");
chipColors.add("green");
}
}
I'm taking this approach so that I can create new chips of the appropriate type by just calling new ChipA()
where I need to, as opposed to new Chip(<long list of arguments that describe Chip A>)
. I need to pass a collection of these chips from one activity to another. I have already solved this problem in my code by storing the Chips I want to pass around globally as described in this article, but the end of the article advises using intent extras for this kind of operation instead. Could someone more clearly explain why? Is it just convention? If it's just a matter of readability, this method seems relatively compact and readable.
From reading around, it's clear that the intended way to pass arbitrary classes between activities using Intent extras is to have them implement Parcelable
. However, since I am working with many subclasses of the class in question, that would mean adding a writeToParcel
and a Parcelable.Creator
to every single subclass. (describeContents()
appears unimportant, and as I understand it I could just implement that in the basic abstract class.) I'm potentially going to have a lot of subclasses here, which will mean adding a lot of repetitive code. Is implementing Parcelable
still considered the preferred method in this case? Am I missing something that would allow me to cut down on redundant code? If at all possible I would prefer to keep my Chip subclasses fairly compact.
Please be gentle, I'm new to both Stack Overflow and Android development.