I am trying to pass an object as a parameter from an activity to a fragment.
I already searched over hill and dale to find out how to do it, I tried many methods but none of them worked.
Here is what I tried :
// In my Activity, where I call my fragment
var bundle = Bundle()
bundle.putParcelable("restaurant", restaurant)
var fragment = Home2Fragment()
fragment.arguments = bundle
supportFragmentManager.beginTransaction().replace(R.id.content, fragment, FRAGMENT_HOME).commit()
My IDE (Android Studio) underline "restaurant" in bundle.putParcelable line, telling me a type mismatch. This is the same with the getArguments() methods, I get a type mismatch.
My class look like this :
@Parcel
class Establishment {
var title = ""
// More variables
fun loadFromJson(json: JsonObject) {
title = readString(json, TAG_TITLE)
// More API loads
}
companion object {
private const val TAG_TITLE = "title"
// More tags
}
}
I don't understand why my class is set as "Parcel" but is not recognized as a "Parcelable".
I am looking for an efficient way to pass parameters from an activity to a fragment.
Thanks a lot in advance.
EDIT :
As asked, I tried to make it as Serializable. I can't make it Serializable, as "Serializable" does not exists.
But I tried to make it "SerializedName", but "This annotation is not applicable to target 'class' "