Android Class Parcelable with ArrayList - android

I have an android project where I have a class. In that class is an ArrayList<Choices>. I will be getting some XML, parsing it out, then making objects out of it which I will be passing to another activity. I'm choosing Parcelable for this.
Is Parcelable a good choice? Am I doing everything correctly? I'm not familiar really with Parcelable. My ArrayList is of another class that I made within this class. Will it properly pass that ArrayList of objects to the Parcel with it not extending Parcelable and stuff?
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.os.ParcelableCompat;
public class Question implements Parcelable{
String id;
String text;
String image;
ArrayList<Choices> CHOICES;
public Question(String id, String text, String image) {
super();
this.id = id;
this.text = text;
this.image = image;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
#Override
public String toString() {
return "Question [id=" + id + ", text=" + text + ", image=" + image
+ "]";
}
// Answer Choices class
class Choices {
boolean isCorrect;
String choice;
public Choices(boolean isCorrect, String choice) {
this.isCorrect = isCorrect;
this.choice = choice;
}
public String getChoice() {
return choice;
}
public boolean getIsCorrect() {
return isCorrect;
}
#Override
public String toString() {
return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
+ "]";
}
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
#Override
public Question createFromParcel(Parcel in) {
return new Question(in);
}
#Override
public Question[] newArray(int size) {
return new Question[size];
}
};
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(CHOICES);
}
private Question(Parcel in) {
this.id = in.readString();
this.text = in.readString();
this.image = in.readString();
this.CHOICES = in.readArrayList(Choices.class.getClassLoader());
}
}
Thanks for any help!

If you need to pass an ArrayList between activities, then I'd go with implementing Parcelable also, as there is no other way around I guess.
However I don't think you will need that much of getters and setters. Here is your Question class which implements Parcelable:
public class Question implements Parcelable {
public String id;
public String text;
public String image;
public ArrayList<Choice> choices;
/**
* Constructs a Question from values
*/
public Question (String id, String text, String image, ArrayList<Choice> choices) {
this.id = id;
this.text = text;
this.image = image;
this.choices = choices;
}
/**
* Constructs a Question from a Parcel
* #param parcel Source Parcel
*/
public Question (Parcel parcel) {
this.id = parcel.readString();
this.text = parcel.readString();
this.image = parcel.readString();
this.choices = parcel.readArrayList(null);
}
#Override
public int describeContents() {
return 0;
}
// Required method to write to Parcel
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(choices);
}
// Method to recreate a Question from a Parcel
public static Creator<Question> CREATOR = new Creator<Question>() {
#Override
public Question createFromParcel(Parcel source) {
return new Question(source);
}
#Override
public Question[] newArray(int size) {
return new Question[size];
}
};
}

Use:
in.createTypedArrayList(Product.CREATOR)
In the constructor that takes a Parable object as a param.
In the writeToParcel method use dest.writeTypedList(product);

You have it almost, but not quite, right. The Question class looks nearly correctly Parcelable. The only thing that won't work is parcelling the array of Choices.
There are two ways that you could do it:
Make Choices Parcelable. You will have to add all the required methods, and the CREATOR. Because Android knows how to parcel ArrayLists of Parcelables, this will work.
Make parceling the Array of Choices part of parcelling the Question. To do this, you'd probably push the size of the array into the Parcel, and then loop over the Choices, pushing their values. On the other end, you'd read the count first, and then read the values for each Choice, creating each and pushing it into the new Question.

Create a new java file for "Choices" and implement "Parcelable". If you do not implement parcelable you will get run-time exception (Unable to Marshal). So use the code below :
public class Choices implements Parcelable{
boolean isCorrect;
String choice;
public Choices(boolean isCorrect, String choice) {
this.isCorrect = isCorrect;
this.choice = choice;
}
//Create getters and setters
protected Choices(Parcel in) {
isCorrect = in.readByte() != 0;
choice = in.readString();
}
public static final Creator<Choices> CREATOR = new Creator<Choices>() {
#Override
public Choices createFromParcel(Parcel in) {
return new Choices(in);
}
#Override
public Choices[] newArray(int size) {
return new Choices[size];
}
};
#Override
public String toString() {
return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
+ "]";
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isCorrect ? 1 : 0));
dest.writeString(choice);
}
}
As mentioned in above answer by #G.Blake you need to make Choices Parcelable and Android knows how to parcel ArrayLists of Parcelables

Related

Accessing and Storing multiple POJO classes in Room

I have Multiple POJO classes as type's for the main POJO class which I am using to parse the json data using Retrofit. So I provided type converters for the first level Pojo but I'm having trouble in reading the stored data because that is not in correct format.
Here is my POJO class
#Entity(tableName = "entry")
public class Entry implements Parcelable {
#NonNull
#PrimaryKey
#SerializedName("id")
private FeedID feedId;
#SerializedName("title")
private Title EntryTitle;
#SerializedName("im:image")
private List<Image> image;
#SerializedName("summary")
private Summary summary;
public Entry(){}
public FeedID getFeedId() {
return feedId;
}
public void setFeedId(FeedID feedId) {
this.feedId = feedId;
}
public Title getEntryTitle() {
return EntryTitle;
}
public void setEntryTitle(Title entryTitle) {
this.EntryTitle = entryTitle;
}
public List<Image> getImage() {
return image;
}
public void setImage(List<Image> image) {
this.image = image;
}
public Summary getSummary() {
return summary;
}
public void setSummary(Summary summary) {
this.summary = summary;
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator<Entry>() {
#Override
public Entry createFromParcel(Parcel in) {
return new Entry(in);
}
#Override
public Entry[] newArray(int size) {
return new Entry[size];
}
};
protected Entry(Parcel in) {
feedId = (FeedID) in.readValue(FeedID.class.getClassLoader());
EntryTitle = (Title) in.readValue(Title.class.getClassLoader());
if (in.readByte() == 0x01) {
image = new ArrayList<Image>();
in.readList(image, Image.class.getClassLoader());
} else {
image = null;
}
summary = (Summary) in.readValue(Summary.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(feedId);
dest.writeValue(EntryTitle);
if (image == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(image);
}
dest.writeValue(summary);
}
public class Title implements Parcelable {
private String label;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
#SuppressWarnings("unused")
public final Parcelable.Creator<Title> CREATOR = new Parcelable.Creator<Title>() {
#Override
public Title createFromParcel(Parcel in) {
return new Title(in);
}
#Override
public Title[] newArray(int size) {
return new Title[size];
}
};
protected Title(Parcel in) {
label = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(label);
}
}
public class Image implements Parcelable {
private String label;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
#SuppressWarnings("unused")
public final Parcelable.Creator<Image> CREATOR = new Parcelable.Creator<Image>() {
#Override
public Image createFromParcel(Parcel in) {
return new Image(in);
}
#Override
public Image[] newArray(int size) {
return new Image[size];
}
};
protected Image(Parcel in) {
label = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(label);
}
}
public class Summary implements Parcelable {
private String label;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
#SuppressWarnings("unused")
public final Parcelable.Creator<Summary> CREATOR = new Parcelable.Creator<Summary>() {
#Override
public Summary createFromParcel(Parcel in) {
return new Summary(in);
}
#Override
public Summary[] newArray(int size) {
return new Summary[size];
}
};
protected Summary(Parcel in) {
label = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(label);
}
}
public class FeedID implements Parcelable {
private Attributes attributes;
public Attributes getAttributes() {
return attributes;
}
public void setAttributes(Attributes attributes) {
this.attributes = attributes;
}
#SuppressWarnings("unused")
public final Parcelable.Creator<FeedID> CREATOR = new Parcelable.Creator<FeedID>() {
#Override
public FeedID createFromParcel(Parcel in) {
return new FeedID(in);
}
#Override
public FeedID[] newArray(int size) {
return new FeedID[size];
}
};
protected FeedID(Parcel in) {
attributes = (Attributes) in.readValue(Attributes.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(attributes);
}
}
public class Attributes implements Parcelable {
#SerializedName("im:id")
private String id;
public String getIm() {
return id;
}
public void setIm(String im) {
this.id = im;
}
#SuppressWarnings("unused")
public final Parcelable.Creator<Attributes> CREATOR = new Parcelable.Creator<Attributes>() {
#Override
public Attributes createFromParcel(Parcel in) {
return new Attributes(in);
}
#Override
public Attributes[] newArray(int size) {
return new Attributes[size];
}
};
protected Attributes(Parcel in) {
id = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
}
}
}
and this is my TypeConverter class
public class RoomTypeConverters {
#TypeConverter
public static String toString(Entry.Title value) {
Gson gson = new Gson();
String json = gson.toJson(value);
return json;
}
#TypeConverter
public static Entry.Title toTitle(String value) {
Type listType = new TypeToken<Entry.Title>() {
}.getType();
return new Gson().fromJson(value, listType);
}
#TypeConverter
public static String toString(Entry.FeedID value) {
Gson gson = new Gson();
String json = gson.toJson(value);
return json;
}
#TypeConverter
public static Entry.FeedID toFeedID(String value) {
Type listType = new TypeToken<Entry.FeedID>() {
}.getType();
return new Gson().fromJson(value, listType);
}
#TypeConverter
public static String toString(Entry.Summary value) {
Gson gson = new Gson();
String json = gson.toJson(value);
return json;
}
#TypeConverter
public static Entry.Summary toSummary(String value) {
Type listType = new TypeToken<Entry.Summary>() {
}.getType();
return new Gson().fromJson(value, listType);
}
#TypeConverter
public static String toString(List<Entry.Image> value) {
Gson gson = new Gson();
String json = gson.toJson(value);
return json;
}
#TypeConverter
public static List<Entry.Image> toImage(String value) {
Type listType = new TypeToken<Entry.Summary>() {
}.getType();
return new Gson().fromJson(value, listType);
}
}
Do I have to create multiple POJO's like that only for one field? Do we have any other alternative?
Yes, to save a POJO you have to define a class for it (either inner or normal class).
But what I understand from your data schema is that you have a possibly complex data store server side and you want to use a subset of that data in your app. Your POJO can be simplified so you can create a Class called EntryRecord for example and define a simple setter and getter for it like this:
#Entity(tableName = "entry")
public class EntryRecord {
#NonNull
#PrimaryKey
#SerializedName("id")
private String feedId;
#SerializedName("title")
private StringEntryTitle;
#SerializedName("im:image")
private String[] image;
#SerializedName("summary")
private String summary;
public EntryRecord(Entry entry){
//Resolve primitive values from Entry class attributes..
}
public Entry getEntry(){
Entry entry = new Entry();
//Resolve entry values from primitive ones...
return entry ;
}
}
, now all you have to do is to use this class to save and restore your data, you can event make your DAO abstract to convert your POJO before save and vice versa.
P.S: if you don't have any use for such complex Schema with so many single variable classes I really advise you to define custom GSON serializer/deserializer to convert the complex POJO to simplified one from the beginning; removing such a complex schema entirely from your code base.
So after a little research I have found a annotation for which if you have multiple inner classes room will take care of it and you don't have to create any TypeConverters for it.
#Embedded Can be used as an annotation on a field of an Entity or Pojo to signal that nested fields (i.e. fields of the annotated field's class) can be referenced directly in the SQL queries.
For example, if you have 2 classes:
public class Coordinates {
double latitude;
double longitude;
}
public class Address {
String street;
#Embedded
Coordinates coordinates;
}
Room will consider latitude and longitude as if they are fields of the Address class when mapping an SQLite row to Address.
Source : Documentation

Android use Intent to send a list of Parcelable objects which contain a list of another Parcelable objects

In my Android app, I want to use Intent to pass a list of SingleGroup to another Activity where each SingleGroup object contains a list of SingleImage, here are class for SingleImage and SingleGroup:
public class SingleImage implements Parcelable{
public String name;
public int drawableId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDrawableId() {
return drawableId;
}
public void setDrawableId(int drawableId) {
this.drawableId = drawableId;
}
public SingleImage(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
public static final Parcelable.Creator<SingleImage> CREATOR
= new Parcelable.Creator<SingleImage>()
{
public SingleImage createFromParcel(Parcel in)
{
return new SingleImage(in);
}
public SingleImage[] newArray(int size)
{
return new SingleImage[size];
}
};
private SingleImage(Parcel in)
{
name = in.readString();
drawableId = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(drawableId);
}
}
Here is SingleGroup class:
public class SingleGroup implements Parcelable{
private static final String TAG = "SINGLEGROUP";
private ArrayList<SingleImage> images = new ArrayList<SingleImage>(0);
private String myTitle;
private String groupDesc;
public SingleGroup(String mTitle, String desc, ArrayList<SingleImage> imagesList) {
this.myTitle = mTitle;
this.groupDesc = desc;
this.images = imagesList;
}
public String getGroupDesc() {
return groupDesc;
}
public String getMyTitle() {
return myTitle;
}
public void setMyTitle(String myTitle) {
this.myTitle = myTitle;
}
public ArrayList<SingleImage> getImages() {
return images;
}
public void setImages(ArrayList<SingleImage> images) {
this.images = images;
}
public static final Parcelable.Creator<SingleGroup> CREATOR
= new Parcelable.Creator<SingleGroup>()
{
public SingleGroup createFromParcel(Parcel in)
{
return new SingleGroup(in);
}
public SingleGroup[] newArray(int size)
{
return new SingleGroup[size];
}
};
private SingleGroup(Parcel in)
{
myTitle = in.readString();
groupDesc = in.readString();
//problem maybe here
in.readTypedList(images,SingleImage.CREATOR);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(myTitle);
dest.writeString(groupDesc);
//problem maybe here
dest.writeTypedList(images);
}
#Override
public String toString() {
String result = "myTitle "+myTitle+" desc "+ groupDesc + " imagesize "+images.size();
return result;
}
}
It works ok when calling to pass ArrayList<SingleImage> through Intent,
while when I called to pass ArrayLlist<SingleGroup> through Intent I got error:
Parcel android.os.Parcel#3c4c6743: Unmarshalling unknown type code
3211379 at offset 224
Any idea? Thanks in advance
---------------- Update ----------------
I have changed in.readTypedList(images, null); to in.readTypedList(images, SingleImage.CREATOR); but it still not working. same unmarshalling error
---------------- Problem Solved --------------
Thanks to Keita. The real problem is not actually listed in my code. When I passing the parcelable object, I should also include some other attributes which I missed.
Strongly recommend AS plugin Android Parcelable code generator
You must be pass SingleImage.CREATOR into method readTypedList instead of "null" value
private SingleGroup(Parcel in)
{
myTitle = in.readString();
groupDesc = in.readString();
//problem maybe here
in.readTypedList(images, SingleImage.CREATOR);
}
Try this one. That is what i always do and it work:
private SingleGroup(Parcel in)
{
myTitle = in.readString();
groupDesc = in.readString();
//problem maybe here
in.createTypedArrayList(SingleImage.CREATOR);
}
ArrayList is serialize. So try to this
public class SingleImage implements Serializable{
....
}
Refer this: ArrayList Android Documentation

Parcelable inheritance in android caused ClassCastExeption

i have a Parent class implementing parcelable and a child extending parent also implementing parcelable. My code is attached below. I get a runtime ClassCastException. What am I missing?
Thanks,
Parent Class:
public class Follower implements Parcelable{
private long id;
private String fullName;
public Follower() {
}
public Follower(String fullName) {
this.fullName = fullName;
}
public long getId(){
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getFullName());
}
protected Follower(Parcel in){
setFullName(in.readString());
}
public static final Parcelable.Creator<Follower> CREATOR =
new Parcelable.Creator<Follower>() {
public Follower createFromParcel(Parcel in) {
return new Follower(in);
}
public Teen[] newArray(int size) {
return new Teen[size];
}
};
}
The Child Class
public class Teen extends Follower
implements Parcelable{
public Teen(){
super();
}
public Teen( String fullName, String birthDate) {
super(fullName);
this.birthDate = birthDate;
}
private String birthDate;
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate){
this.birthDate = birthDate;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString (birthDate);
}
private Teen(Parcel in){
super(in);
birthDate = in.readString();
}
public static final Parcelable.Creator<Teen> CREATOR =
new Parcelable.Creator<Teen>() {
public Teen createFromParcel(Parcel in) {
return new Teen(in);
}
public Teen[] newArray(int size) {
return new Teen[size];
}
};
}
On the server, I create a Teen/Follower like below. The teen and follower clasess are copied to the server without the parcelable implementation.
public Follower getData()
{
if (logic to see if the user is a teen){
Teen t = new Teen( "Full Name", "1:1:1960");
return t;
}
else
{
Follower f = new Follower("Full Name");
return f;
}
}
On the client
I do get the Follower in the client code. I have checked by accessing the Follower class methods. It works. However when I do something like this I get a ClassCastException at runtime.
if(logic to see if it is a teen)
{
Teen t = (Teen) follower;
}
I am sure it is something small that I am missing but am at loss. Any help will be appreciated. Thanks,
Change this:
public class Teen extends Follower
implements Parcelable{
to this:
public class Teen extends Follower{
You don't have to reimplement the parcelable in child, because it inherit from the parent. I'm not sure, but when you create a Teen it implements it's own parcelable data-type, and it isn't in hierarchical structure with it's parent parcelable data-type.

What is the correct way of creating a parcelable?

I have created the following parcelable object:
public class ViewModel implements Parcelable {
private String image, price, credit, title, description, id;
public ViewModel(String image, String price, String credit, String title, String description, String id) {
this.image = image;
this.price = price;
this.credit = credit;
this.title = title;
this.description = description;
this.id = id;
}
public String getPrice() {
return price;
}
public String getCredit() {
return credit;
}
public String getDescription() {
return description;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {
this.image,
this.price,
this.credit,
this.title,
this.description,
this.id
});
}
/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<ViewModel> CREATOR = new Parcelable.Creator<ViewModel>() {
public ViewModel createFromParcel(Parcel pc) {
return new ViewModel(pc);
}
public ViewModel[] newArray(int size) {
return new ViewModel[size];
}
};
/**Creator from Parcel, reads back fields IN THE ORDER they were written */
public ViewModel(Parcel pc){
image = pc.readString();
price = pc.readString();
credit = pc.readString();
title = pc.readString();
description = pc.readString();
id = pc.readString();
}
}
Now I am sending an ArrayList of ViewModel through bundle:
bundle.putParcelableArrayList("products", viewModels);
Is there anything wrong I am doing? Because I get null Bundle Arguments, but if I send a simple string, everything works.
make this change:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.image);
dest.writeString(this.price);
dest.writeString(this.credit);
dest.writeString(this.title);
dest.writeString(this.description);
dest.writeString(this.id);
}
getIntent().getParcelableArrayListExtra("product"); // get the list
You are trying to retrieve a parcelable ArrayList. You need to retrive a parcelable ViewModel object that you created. Change:
bundle.putParcelableArrayList("product", viewModels);
and
getIntent().getParcelableArrayList("product");
to:
bundle.putParcelable("product", viewModels);
and
getIntent().getParcelable("product");

Nested parcelable object not reading

I'm trying to parcel an object which contains some string/int variables and an object variable. The strings and int's are working, but not the nested object. I understand I would have to it also parcelable, but I'm apparently doing something wrong=. In my nested class, the writeToParcel method gets called (I check with a Log.d() call), but the createFromParcel() doesn't. I end up getting a null object. This is my simplified code:
public class MyClass implements Parcelable {
public MyClass() {
}
private Integer id;
private String name;
private MyOtherClass otherClass = new MyOtherClass();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OtherClass getOtherClass() {
return otherClass;
}
public void setOtherClass(OtherClass otherClass) {
this.otherClass = otherClass;
}
public static final Parcelable.Creator<MyClass> CREATOR
= new Parcelable.Creator<MyClass>() {
public MyClass createFromParcel(Parcel in) {
return new MyClass(in);
}
public MyClass[] newArray(int size) {
return new MyClass[size];
}
};
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
dest.writeParcelable(otherClass, flags);
}
private MyClass(Parcel in) {
name = in.readString();
id = in.readInt();
otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());
}
}
class MyOtherClass implements Parcelable {
public OtherClass() {
}
private String resourcePreviewURL;
public String getResourcePreviewURL() {
return resourcePreviewURL;
}
public void setResourcePreviewURL(String resourcePreviewURL) {
this.resourcePreviewURL = resourcePreviewURL;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
Log.d("parcel", "write to parcel"); // this gets called
out.writeString(resourcePreviewURL);
}
public static final Parcelable.Creator<MyOtherClass> CREATOR
= new Parcelable.Creator<MyOtherClass>() {
public MyOtherClass createFromParcel(Parcel in) {
Log.d("parcel", "create from parcel"); // this doesn't get called
return new MyOtherClass(in);
}
public ResourcePreviews[] newArray(int size) {
return new ResourcePreviews[size];
}
};
private OtherClass(Parcel in) {
Log.d("parcel", "read from parcel"); // this doesn't get called
resourcePreviewURL = in.readString();
}
}
I solved this by changing the order in which I write/read from the Parcel. I made the dest.writeParcelable(otherClass, flags); and the otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader()); calls to be the first in their methods and it started working. Is that an issue?
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(otherClass, flags);
dest.writeString(name);
dest.writeInt(id);
}
private MyClass(Parcel in) {
otherClass = (OtherClass) in.readParcelable(OtherClass.class.getClassLoader());
name = in.readString();
id = in.readInt();
}

Categories

Resources