2

I'm working with an Android Project where i use both Java and Kotlin together, I have a Kotlin Data Class with Parcelable implemented (Some properties in this class are custom Java POJO Class and it's Parcelable implemented too).

When I try to pass this class as an Intent Parcelable Extra in compile-time there are no errors, but when I try to retrieve this class in another Activity from Intent.getExtras().getParcelable("Key") it throws an exception

E/Parcel: Class not found when unmarshalling: 
    java.lang.ClassNotFoundException

So I tried to search for the cause of the exception and tried all the Answers in this question and nothing worked

1 - Koltin data class


data class OrderResponse (
    @SerializedName("client")
    var client: RideResponse? = null,

    @SerializedName("driver")
    var driver: BigRideResponse? = null,

    @SerializedName("id")
    var id: String? = null,
    @SerializedName("rideInfo")
    var rideInfo: RideDetails? = null
) : Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readParcelable(RideResponse::class.java.classLoader),
            parcel.readParcelable(BigRideResponse::class.java.classLoader),
            parcel.readString(),
            parcel.readParcelable(RideDetails::class.java.classLoader)) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeParcelable(client, flags)
        parcel.writeParcelable(driver, flags)
        parcel.writeString(id)
        parcel.writeParcelable(rideInfo, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<OrderResponse> {
        override fun createFromParcel(parcel: Parcel): OrderResponse {
            return OrderResponse(parcel)
        }

        override fun newArray(size: Int): Array<OrderResponse?> {
            return arrayOfNulls(size)
        }
    }
}

2- The RideResponse class


public class RideResponse implements Parcelable {

    private String id;

    private User user;

    private OrderLocation location;

    private RideInfo rideInfo;

    private String orderType;

    private String repeatedDays ;

    private boolean repeated;

    public boolean isRepeated() {
        return repeated;
    }

    public void setRepeated(boolean repeated) {
        this.repeated = repeated;
    }

    public String getRepeatedDays() {
        return repeatedDays;
    }

    public void setRepeatedDays(String repeatedDays) {
        this.repeatedDays = repeatedDays;
    }




    public RideResponse() {
    }


    protected RideResponse(Parcel in) {
        id = in.readString();
        user = in.readParcelable(User.class.getClassLoader());
        location = in.readParcelable(OrderLocation.class.getClassLoader());
        rideInfo = in.readParcelable(RideInfo.class.getClassLoader());
        orderType = in.readString();
        repeatedDays = in.readString();
        repeated = in.readByte() != 0;

    }

    public static final Creator<RideResponse> CREATOR = new Creator<RideResponse>() {
        @Override
        public RideResponse createFromParcel(Parcel in) {
            return new RideResponse(in);
        }

        @Override
        public RideResponse[] newArray(int size) {
            return new RideResponse[size];
        }
    };

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public OrderLocation getLocation() {
        return location;
    }

    public void setLocation(OrderLocation location) {
        this.location = location;
    }

    public RideInfo getRideInfo() {
        return rideInfo;
    }

    public void setRideInfo(RideInfo rideInfo) {
        this.rideInfo = rideInfo;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(id);
        dest.writeParcelable(user, flags);
        dest.writeParcelable(location, flags);
        dest.writeParcelable(rideInfo, flags);
        dest.writeString(orderType);
        dest.writeString(repeatedDays);
        dest.writeByte((byte) (repeated ? 1 : 0));

    }

}

3- BigRideResponse is the same as RideResponse but with extra fields

4- Code passing the parcelable

Intent intent = new Intent(getContext(), RatingActivity.class);
        intent.putExtra(ApplicationConstants.ExtrasTag.ORDER_RESPONSE, mOrderResponse);
        getViewCallback().openRatingScreen(intent, REQUEST_CODE_RATE);

5 - Code retrieving the parcelable

mOrderResponse = intent.getExtras().getParcelable(ApplicationConstants.ExtrasTag.ORDER_RESPONSE);

And after some time-wasting, I tried to convert the Kotlin Data Class(OrderResponse) to a regular Java POJO and implemented Parcelable and boom it worked fine !!

So I'm guessing that using Parcelable data classes with Java Bundles doesn't seem to work so I wanted to know why exactly? Isn't kotlin Classes mapped to Java Classes so they are the same?

Ahmed Elshaer
  • 364
  • 7
  • 21

1 Answers1

1

You need to send object as Parcelable to send the Data class objects via Intent like:

Here i am using Kotlin to create intent object

val intent = Intent(context, RatingActivity::class.java)
intent.putParcelableArrayListExtra(ApplicationConstants.ExtrasTag.ORDER_RESPONSE, mOrderResponse)

And now need to get the result as Parcelable like this.

Make sure create object of Data Class like this and get the Intent value in the object

//object create of ArrayList
    private var mOrderResponseViaIntent= ArrayList<OrderResponse>()


    mOrderResponseViaIntent= intent.getParcelableArrayListExtra<OrderResponse>(ApplicationConstants.ExtrasTag.ORDER_RESPONSE)
Pawan Soni
  • 860
  • 8
  • 19
  • I already know that and my use-case is when I'm using data classes as Parcelable in Java Activity that was the issue, please read the question and let me know if you don't understand it – Ahmed Elshaer Aug 25 '19 at 10:59
  • Yes!! understand your concept.. The issue is, you are trying to get the data class(Which is concept of kotlin) in simple java class. So java class will not aware with the Data class because of Kotlin Lanuguage(obviousy will through exception). You must need kotlin code to get the data class in parcelable intent. – Pawan Soni Aug 26 '19 at 17:18