Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass an object from one activity to another

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.

like image 758
Maria Avatar asked Nov 12 '22 12:11

Maria


1 Answers

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.

like image 122
wtsang02 Avatar answered Nov 15 '22 05:11

wtsang02