How this class implements parcelable right. i cant do parcel Object type.String, Double,Integer,.. variables types sets to the Object type.
public class MyClass implements Parcelable {
public String Name;
public Object Value; //variables type to set
protected MyClass(Parcel in) {
Name = in.readString();
//Value = in.?
}
public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
@Override
public MyClass createFromParcel(Parcel in) {
return new MyClass(in);
}
@Override
public MyClass[] newArray(int size) {
return new MyClass[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
//dest.
}
}
Probably I am too late but this might help someone else looking for same.
Since the Object class does not implement Parcelable, a workaround is needed. A way which i got through this obstacle was by using GSON and serializing and de-serializing to String and from String.
For eg:
public class MyClass implements Parcelable {
public String Name;
public Object Value; //variables type to set
protected MyClass(Parcel in) {
Name = in.readString();
Value = new Gson().fromJson(in.readString(), Object.class);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeString(new Gson().toJson(Value));
}
}
The Parcel
documentation is quite explicit about what you can do, here is some idea.
You have the methods writeParcelable
to do that, but you can't parcel an Object
because Object
doesn't implement Parcelable
.
You need to define a class
that implements Parcelable
for your value
.
public class MyClass implements Parcelable {
public String name;
public ParcelableValue value; //variables type to set
...
}
public class ParcelableValue implements Parcelable {
...
}
For a complete implementation, see Write a sub class of Parcelable to another Parcel
You also have writeValue
but this still have some condition :
For the complete list, see the doc of the method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.