Passing objects that extend an abstract class between actions

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; } //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 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.

+4
source share
1 answer

You need nothing; Parcelable is the preferred way to do this.
I agree that this leads to some code template, but this is a clean way to handle this.

You can skip this by extending the Application class, but it will lead to some complications: the Application object may be deleted in some cases, and this will lead to very unpleasant errors. It’s easier to just handle it as intended and create a Constructor (Parcel) for each of the objects that you move in this way.

Another point is that the Singleton template itself is increasingly criticized. I will not go into details, these discussions cover this topic:
https://softwareengineering.stackexchange.com/questions/40373/so-singletons-are-bad-then-what
What's so bad about singles?

Even worse on Android, you can't even count on a real singleton; You do not have insurance that the application object that you are managing (or the Singleton you created) will retain its state through the application life cycle. (random access: http://portabledroid.wordpress.com/2012/05/04/singletons-in-android/ )

Therefore, please do not use Singleton as a way to avoid having to write a couple of Parcels. He will come back and bite you in the ass later.

Edit: here is a good explanation of how to reproduce a terrible random application class: http://www.developerphil.com/dont-store-data-in-the-application-object/

+2
source

All Articles