How to pass object between activities in android using Kotlin?
Arun Aditya
Android Engineer @ Mercedes-Benz | Specializing in Android, Kotlin, and Java | MVVM & Clean Code Advocate | Building efficient mobile apps
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
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
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
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
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 ???