This project demonstrates the usage of Serializable and Parcelable interfaces in Android to pass data between activities.
This app showcases the usage of Serializable and Parcelable interfaces for data serialization and deserialization in Android. It includes two data classes: PersonWithSerializable and PersonWithParcelable. The MainActivity allows you to send data using either Serializable or Parcelable, and the GetDataActivity retrieves the data and displays it on the screen.
- Clone the repository: git clone https://github.com/yash30401/Serializable_And_Parcelable.git
- Open the project in Android Studio.
- Run the app on an Android device or emulator.
- The MainActivity will be displayed, featuring two buttons: "Send Data (Serializable)" and "Send Data (Parcelable)".
- Click on the "Send Data (Serializable)" button to send data using Serializable.
binding.btnSendDataSerializable.setOnClickListener {
val person = PersonWithSerializable(2, "Yashveer Singh", "Delhi", 20)
val intent = Intent(this, GetDataActivity::class.java)
intent.putExtra("PERSON_S", person)
startActivity(intent)
}
- Click on the "Send Data (Parcelable)" button to send data using Parcelable.
binding.btnSendDataParcelable.setOnClickListener {
val person = PersonWithParcelable(1,"Yash","Noida",20)
val intent = Intent(this, GetDataActivity::class.java)
intent.putExtra("PERSON_P",person)
startActivity(intent)
}
- The GetDataActivity will be launched, retrieving and displaying the sent data.
private fun getSerializableData() {
val person = bundle?.getSerializable("PERSON_S") as? PersonWithSerializable
person?.let {
binding.tvId.text = "Id: ${it.id.toString()}"
binding.tvName.text = "Name: ${it.name.toString()}"
binding.tvAddress.text = "Address: ${it.address.toString()}"
binding.tvAge.text = "Age: ${it.age.toString()}"
}
}
GetDataActivity code snippet for retrieving Parcelable data
private fun getParcelableData(){
val person = bundle?.getParcelable<PersonWithParcelable>("PERSON_P")
person?.let {
binding.tvId.text = "Id: ${it.id.toString()}"
binding.tvName.text = "Name: ${it.name.toString()}"
binding.tvAddress.text = "Address: ${it.address.toString()}"
binding.tvAge.text = "Age: ${it.age.toString()}"
}
}
data class PersonWithSerializable(
val id: Int? = 0,
val name: String? = "",
val address: String? = "",
val age: Int? = 18
) : Serializable
data class PersonWithParcelable(
val id: Int? = 0,
val name: String? = "",
val address: String? = "",
val age: Int? = 18
): Parcelable {
// Parcelable implementation code
// ...
}