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?