How to Pass a Serializable Object from One Activity to Another Activity in Android?

05 Aug, 2021
While developing an application in Android, the developer can create more than one activity in the application. So, there can be many activities and can be a case to transfer data from one activity to the other. In such cases, Intents are used. Intents let the user jump from one activity to the other, or go from the current activity to the next activity. While going from one activity to the other, we can pass parameters like strings, integers, etc from the current activity and fetch them in the next activity.
In this article, we will show you how you can create a serializable object, pass it from one activity and fetch it in the next activity.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Create a serializable object (MyCustomObject.kt)
Kotlin
import java.io.Serializable  class MyCustomObject: Serializable {      var name = ""    var id = 0    var place = ""      constructor(mName: String, mId: Int, mPlace: String){        name = mName        id = mId        place = mPlace    }      constructor()}
Step 3: Add a button in the layout of the first activity (activity_main.xml)
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <Button        android:id="@+id/btnSend"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Send" />  </RelativeLayout>
Step 4: Program to pass the object to a new activity (MainActivity.kt)
Kotlin
import android.content.Intentimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.EditTextimport java.io.Serializable  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val btn = findViewById<Button>(R.id.btnSend)          btn.setOnClickListener {            val intent = Intent(this, SecondActivity::class.java)            val passingObject = MyCustomObject()            passingObject.name = "Geek"            passingObject.id = 1            passingObject.place = "India"            intent.putExtra("object", passingObject)            startActivity(intent)        }    }}
Step 5: Create a new activity SecondActivity.kt to catch the passed object and add a TextView in the layout of the second activity activity_second.xml
Kotlin
XML
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.TextView  class SecondActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_second)          val myIntent = intent        val derivedObject = myIntent.getSerializableExtra("object") as MyCustomObject          val myTextView = findViewById<TextView>(R.id.tv1)        myTextView.append(derivedObject.name + "\n")        myTextView.append(derivedObject.id.toString() + "\n")        myTextView.append(derivedObject.place + "\n")    }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">      <TextView        android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_centerInParent="true"/>  </RelativeLayout>
Output:
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
How to Add Views Dynamically and Store Data in Arraylist in Android?
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android