I want to pass two values to another activity can I do this with putExtra or do I have to do it a more complicated way, which it seems from my reading. E.g.. can something l
For a single small amount of value putExtra is perfect but when you pass multiple values and need to pass custom ArrayList or List then I think it's a bad idea. Solution is you can use Parcelable. You and also use Singleton or Serializable also.
Parcelable:
Parcelable class which you need to share.
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 CREATOR
= new Parcelable.Creator() {
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();
}
}
Form your activity where you want to send
MyParcelable example = new MyParcelable();
.....
.....
Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));
From another activity where you want to receive
MyParcelable example = Parcels.unwrap(getIntent().getParcelableExtra("example"));