1

Is it possible to pass an arraylist with arraylists of objects through an intent?

If so, how could you do it? Is it still Parcelable.

So I have arraylist of objects that I'm putting into my main array, and I need that data in the next activity or intent.

Thanks!

huey77
  • 643
  • 1
  • 9
  • 24
  • 1
    Android Intents are not supposed to carry a large amount of data. You could save that array of arrays in somewhere and retrieve them in the second activity. – frogatto Feb 25 '16 at 20:44
  • Possible duplicate of [Intent putExtra ArrayList](http://stackoverflow.com/questions/18050030/intent-putextra-arraylistnamevaluepair) – Sebastian Walla Feb 25 '16 at 20:54
  • Possible duplicate of [Android Parcelable Problem with array](http://stackoverflow.com/questions/7166770/android-parcelable-problem-with-array) – Varun Feb 25 '16 at 21:10
  • Like I stated, I want to pass an arraylist that has arraylists in it! Those arraylists are object arraylists. So it a single arraylist with arraylists in it. Thanks! – huey77 Feb 25 '16 at 21:12

1 Answers1

1

of course this is totally do able:

create a class which implements Parcelable:

public class SampleObject implements Parcelable {

    public String name;

    public SampleObject(){}

    public SampleObject(Parcel source){
        this.name  = source.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

    public static final Parcelable.Creator<SampleObject> CREATOR = new Parcelable.Creator<SampleObject>(){
        @Override
        public SampleObject createFromParcel(Parcel source) {
            return new SampleObject(source);
        }

        @Override
        public SampleObject[] newArray(int size) {
            return new SampleObject[size];
        }
    } ;
}

then when creating the Intent pass as parcelable:

Intent myIntent = new Intent(MainActivity.this, NewIntent.class);
// pass the list here, im using a new List as a sample
myIntent.putParcelableArrayListExtra("NAME", new ArrayList<SampleObject>());
startActivity(myIntent);
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • A similar easy way is to use Serializable - see http://stackoverflow.com/questions/14333449/passing-data-through-intent-using-serializable – Doron Yakovlev Golani Feb 25 '16 at 20:52
  • 1
    @DoronYakovlev-Golani correct, you can do this but android recommends Parcelable as it is better for performance. Serializable is slow compared to Parcelable – kandroidj Feb 25 '16 at 20:53
  • I agree that performance wise you are absolutely correct. However from code the simplicity and maintenance perspectives, Serializable has many advantages. – Doron Yakovlev Golani Feb 25 '16 at 21:02
  • The example above seems to pass an arraylist of objects. I want to pass an arraylist with arraylists in it. And those arraylist are object arraylists. Thanks! – huey77 Feb 25 '16 at 21:11
  • @huey77 can you show me a code sample. And i will update the answer for your needs – kandroidj Feb 27 '16 at 00:19
  • thanks, but I went a different way about handling my situation. Thanks though. @inner_class7 – huey77 Mar 02 '16 at 20:32