How to pass object between activities in android using Kotlin?

How to pass object between activities in android using Kotlin?

We can pass primitive data (Int, Float, Double, String etc.) by using Intent.

// send data
val intent = Intent(context, NextActivity::class.java)
intent.putExtra("name", "Arun Aditya")
context.startActivity(intent)

// receive data
val name = intent.getStringExtra("name")        

You can pass an object between activities in Android using Parcelable or Serializable interface.

1. Serializable

  • Serializable is a markable interface or we can call as an empty interface. It doesn’t have any pre- implemented method.
  • Convert an object to byte stream.
  • Serializable is standard java interface.
  • Creation and passing data are very easy but it is slow process as compared to Parcelable.
  • Serializable is a good choice to use when you want to store the state of an object to a file, a database or over a network.

Follow these steps to use Serializable.

Step 1-> make sure your object class implement the serializable

data class DummyObject(val name: String): Serializable        

Step 2-> In the first activity, put the object in an Intent and start the second activity.

val intent = Intent(this, NextActivity::class.java)
val dummyObject = DummyObject("Arun Aditya")
intent.putExtra("object", dummyobject)
startActivity(intent)        

Step 3 -> In Second Activity, get the object from the intent.

val object = intent.getSerializableExtra("object") as DummyObject        

2. Parcelable

  • More efficient and faster.
  • Directly marshals and unmarshals the data to and from memory.
  • Parclable is specific to Android and is the recommended method.

Follow these steps to use Parcelable.

Step 1-> Make your object class implement the Parcelable interface.

data class MovieData(
    val sr_no: Int = 0,
    val name: String = "",
    val generic: String = "",
    val year: String = "",
    val length: String = "",
    val director: String = "",
    val decp: String = "",
    val url: String = ""
    ): Parcelable{
    constructor(parcel: Parcel) : this(
        parcel.readInt(),
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(sr_no)
        parcel.writeString(name)
        parcel.writeString(generic)
        parcel.writeString(year)
        parcel.writeString(length)
        parcel.writeString(director)
        parcel.writeString(decp)
        parcel.writeString(url)
    }

    override fun describeContents(): Int {
        return 0
    }

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

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

}        

Step 2-> In the first activity, put the object in an Intent and start the second activity.

val intent = Intent(context, DetailsActivity::class.java)
list = setData.movieList()
intent.putExtra("movie", list[0])
startActivity(intent)        

Step 3 -> In Second Activity, get the object from the intent.

val movie = intent.getParcelableExtra<MovieData>("movie")        

If you need to transfer data between activities in Android, use Parcelable for faster and more efficient data transfer, while if you need to store data in a more general way, you can use Serializable.

#ArunAditya #Android #AndroidApplication developer

Himanshu Patel

SDE - l @Unorg | ex- Wappgo(Legal 251) | Kotlin | MVVM | MVI | Android Development | Jet Pack Compose | KMM

6 个月

A very beautiful class called Gson by creating the object of it and simply use their functions will help to passes the data between one activity to another and same to fragments as well

回复
Eduardo Jaramillo

Native Android | Kotlin | Java | Compose | Firebase | XML | Junit | Dagger 2 | Dagger-Hilt | mockk |mockito| MVI | MVVM | UDF(Unidireccional Data Flow) | MVP | Clean-Architecture | Jira | Rest API | Storage | Navigation

6 个月

Why implement interface parcelable ??? We can only add the annotation Parcelable or not ???

回复

要查看或添加评论,请登录

Arun Aditya的更多文章

社区洞察

其他会员也浏览了