Skip to content

Instantly share code, notes, and snippets.

@mdrabic
Created November 5, 2013 02:25
Show Gist options
  • Save mdrabic/7312820 to your computer and use it in GitHub Desktop.
Save mdrabic/7312820 to your computer and use it in GitHub Desktop.
The Bundle holds Key<String> Value<Parcelable> pairs and provides convenience methods for several of the primitive wrapper types such as Short, Byte, Integer, etc. In the event your data does not easily fit into one of these methods, you can implement the Parcelable interface and use Bundle.putParcelable(key,value).
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* POJO to hold contents of a Mail. Implements Parcelable so that it can be used in the bundle to
* save and restore information to the UI entered by the user in a Activity.
*/
public class Mail implements Parcelable {
private String mFrom;
private String mTo;
private String mSubject;
private String mBody;
public Mail(String from, String to, String subject, String body) {
mFrom = from == null ? "" : from;
mTo = to == null ? "" : to;
mSubject = subject == null ? "" : subject;
mBody = body == null ? "" : body;
}
public String getFrom() {
return mFrom;
}
public String getTo() {
return mTo;
}
public String getSubject() {
return mSubject;
}
public String getBody() {
return mBody;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
List<String> values = new ArrayList<String>();
values.add(mFrom);
values.add(mTo);
values.add(mSubject);
values.add(mBody);
dest.writeStringList(values);
}
public static final Creator<Mail> CREATOR = new Creator<Mail>() {
@Override
public Mail createFromParcel(Parcel source) {
List<String> values = new ArrayList<String>();
source.readStringList(values);
return new Mail(values.get(0), values.get(1), values.get(2), values.get(3));
}
@Override
public Mail[] newArray(int size) {
return new Mail[size];
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment