0

So this is my class:

class Pricecard(val img: Bitmap?, val title: String?, val weight: String?, val price: String?, val code: String?) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readValue(),   //this is giving an error
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(img)  //this is giving an error
        parcel.writeString(title)
        parcel.writeString(weight)
        parcel.writeString(price)
        parcel.writeString(code)
    }

    override fun describeContents(): Int {
        return 0
    }

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

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

I want to know how to pass the Bitmap image to this activity so I can show it with the other variables from the object. This has to be in a list, because there are multiple pricecards that I want to show.

I tried to use Parcelable but it did not work because of those errors. I expected it to work properly.

Meo11
  • 3
  • 4
  • 1
    Do not pass around `Bitmap` .. Save it in app's local storage and pass around the file URL . Having Bitmaps in memory can lead to OOM error.. This solution might seems a big change but its the right way .. – ADM Jul 04 '23 at 08:57
  • Okay thank you for your quick response. So I have to change the first variable from a Bitmap to a string and pass around the file URL there, so I can find the Bitmap image again with the string URL that i passed around? – Meo11 Jul 04 '23 at 09:04
  • An alternative is to create an "in memory cache". Meaning you create a class where the same instance can be injected into both Activities. Then Activity A stores the bitmap into the cache, and Activity B retrieves it. Like that you don't need to store anything into a local storage and the Bitmap will be gone when you close the app. – Eselfar Jul 05 '23 at 13:30

0 Answers0