I am trying to learn Android development by creating a helper program for the board game I play. I came across a situation very similar to the one answered by Skip custom object between actions . The difference in my situation is that all user objects extend the abstract class.
The abstract class is a chip (comparable to card gameplay) and looks like this:
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; }
An example of a class that extends 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 use this approach to create new chips of the appropriate type simply by calling new ChipA() where I need, and not new Chip(<long list of arguments that describe Chip A>) . I need to transfer a collection of these chips from one activity to another. I already solved this problem in my code, saving the chips that I want to transfer globally, as described in this article , but at the end of the article it is recommended to use additional intentions for this kind. Can someone explain more clearly why? Is this just a convention? If this is simply a matter of readability, this method seems relatively compact and readable.
From the reading it is clear that the proposed way to transfer arbitrary classes between actions using the Intent options is to implement Parcelable . However, since I work with many subclasses of the class in question, this would mean adding a writeToParcel and a Parcelable.Creator to each subclass. ( describeContents() seems inconsequential, and as I understand it, I could just implement this in a base abstract class.) I will potentially have many subclasses, which would mean adding a lot of repeating code. Does Parcelable preferred method in this case? Am I missing something that would allow me to reduce redundant code? If at all possible, I would prefer my Chip subclasses to be fairly compact.
Please be careful, I'm new to both Android and Android.
source share