简体   繁体   中英

how to parcel Object type in Parcelable class in Java

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));
    }

}
  • When writing to parcel converting the Object (Serializing) using GSON to JSON String and writing it as a String to parcel.
  • While reading from parcel reading the parcel as String and converting (Deserializing) it to Object class using Gson again.

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 :

  • Any object that implements Serializable (but see writeSerializable(Serializable) for caveats). Note that all of the previous types have relatively efficient implementations for writing to a Parcel; having to rely on the generic serialization approach is much less efficient and should be avoided whenever possible.*

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM