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!

4
  • 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. Commented Feb 25, 2016 at 20:44
  • Possible duplicate of Intent putExtra ArrayList<NameValuePair> Commented Feb 25, 2016 at 20:54
  • Possible duplicate of Android Parcelable Problem with array Commented Feb 25, 2016 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! Commented Feb 25, 2016 at 21:12

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

A similar easy way is to use Serializable - see stackoverflow.com/questions/14333449/…
@DoronYakovlev-Golani correct, you can do this but android recommends Parcelable as it is better for performance. Serializable is slow compared to Parcelable
I agree that performance wise you are absolutely correct. However from code the simplicity and maintenance perspectives, Serializable has many advantages.
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 can you show me a code sample. And i will update the answer for your needs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.