5

I have a file in one of my activities and I write to it (something like a log file). I would like to pass it to another activity and append some other informations to it. How can I do it? I've heard about Parcelable objects but I dn't know if that is the right solution.

Maria
  • 89
  • 1
  • 7
  • 1
    http://stackoverflow.com/questions/5860346/pass-custom-object-between-activities?rq=1 – R3D3vil Jan 28 '13 at 15:50
  • another solution is to extend Application class. http://stackoverflow.com/questions/4572338/extending-application-to-share-variables-globally – StarsSky Jan 28 '13 at 15:53

1 Answers1

1

Storing variables in Appicaltion Class is NOT a good OOP concept. Usually is done by Parcelable as you already mention, this is an example of your model class implementing it:

    public class NumberEntry implements Parcelable {

        private int key;
        private int timesOccured;
        private double appearRate;
        private double forecastValue;

        public NumberEntry() {

            key = 0;
            timesOccured = 0;
            appearRate = 0;
            forecastValue = 0;
        }
    public static final Parcelable.Creator<NumberEntry> CREATOR = new Parcelable.Creator<NumberEntry>() {
            public NumberEntry createFromParcel(Parcel in) {
                return new NumberEntry(in);
            }

            public NumberEntry[] newArray(int size) {
                return new NumberEntry[size];
            }
        };
/**
     * private constructor called by Parcelable interface.
     */
    private NumberEntry(Parcel in) {
        this.key = in.readInt();
        this.timesOccured = in.readInt();
        this.appearRate = in.readDouble();
        this.forecastValue = in.readDouble();
    }

    /**
     * Pointless method. Really.
     */
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.key);
        dest.writeInt(this.timesOccured);
        dest.writeDouble(this.appearRate);
        dest.writeDouble(this.forecastValue);
    }

However, Parcelable as others say is a bad design itself, so if you do not experience performace issues, implementing Serializable is also another option.

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • Thanks, may be I should try both solutions. – Maria Jan 28 '13 at 16:10
  • @wtsang02 Exactly. My feeling is that the truly _best_ solution depends on the lifetime, scope and nature of what needs to be stored. The details are not contained in the question, though. – class stacker Jan 29 '13 at 11:05