java - Passing objects extending an abstract class between activities -


i attempting learn android development making helper program board game play. have run situation similar 1 answered in pass custom object between activities. difference in situation custom objects in question extending abstract class.

the abstract class represents chip (comparable card gameplay wise), , 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 need deal colors/types when there } 

an example of class 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 approach can create new chips of appropriate type calling new chipa() need to, opposed new chip(<long list of arguments describe chip a>). need pass collection of these chips 1 activity another. have solved problem in code storing chips want pass around globally described in this article, end of article advises using intent extras kind of operation instead. more explain why? convention? if it's matter of readability, method seems relatively compact , readable.

from reading around, it's clear intended way pass arbitrary classes between activities using intent extras have them implement parcelable. however, since working many subclasses of class in question, mean adding writetoparcel , parcelable.creator every single subclass. (describecontents() appears unimportant, , understand implement in basic abstract class.) i'm potentially going have lot of subclasses here, mean adding lot of repetitive code. implementing parcelable still considered preferred method in case? missing allow me cut down on redundant code? if @ possible prefer keep chip subclasses compact.

please gentle, i'm new both stack overflow , android development.

you not missing anything, parcelable preferred way this.
agree leads boilerplate code, that's clean way handle this.

it possible skip extending application class lead complications : application object can deleted in circumstances , lead troublesome bugs. way easier handle intended way , create constructor (parcel) each of objects moving way.

another point singleton pattern more , more criticized. won't go details, these discussions cover topic :
https://softwareengineering.stackexchange.com/questions/40373/so-singletons-are-bad-then-what
what bad singletons?

what's worse in android, can't count on real singleton; don't have insurance application object manipulating (or singleton have created yourself) retain state through lifecycle of app. (random ref : http://portabledroid.wordpress.com/2012/05/04/singletons-in-android/)

so please don't use singleton way avoid having write couple of parcels. come , bite in ass later.

edit : here explanation of how reproduce dreaded application class crash : http://www.developerphil.com/dont-store-data-in-the-application-object/


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -