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.

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

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

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

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.

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

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

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

#ArunAditya #Android #AndroidApplication developer

Himanshu Patel

Android Developer | ex- UnORG | Kotlin | MVVM | MVI | Android Development | Jetpack Compose | Kotlin Multiplatform/Compose Multiplatform

1y

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

Like
Reply
Eduardo Jaramillo

Native Android | Kotlin | Java | Compose | Firebase | XML |Dagger| mockk |mockito| MVI | MVVM | UDF | MVP | Clean-Architecture | Jira | Rest API | Storage | Navigation|JUnit|Robolectric

1y

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

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics