I know putExtra can be used to pass objects/strings around between actives. But I am trying you put an ArrayList
of objects like ArrayList<Foo>
in putExtra
Is this possible?
I know putExtra can be used to pass objects/strings around between actives. But I am trying you put an ArrayList
of objects like ArrayList<Foo>
in putExtra
Is this possible?
You can use
intent.putParcelableArrayListExtra()
for passing Arraylist in intent.
EDIT : one more link : Help with passing ArrayList and parcelable Activity
No it isn't. You'll need to serialize
your object into some kind of string representation. One possible string representation is JSON, and one of the easiest ways to serialize to/from JSON in android, if you ask me, is through Google GSON
.
Also if you're just passing objects around then Parcelable
was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).
From Docs :
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Only for very limited and particular types of "Foo". If i recall correctly Double and Long (or maybe it was Integer?) being those types. There might be a way to smuggle a more generic ArrayList through by encapsulating it in some Serializable Object, but I'm not sure about that.