Send Data From one Activity to Another Using Parcelable in Kotlin

In this article, we will learn how to send data from one Activity to Another using Parcelable in Kotlin. Parcelable is a pure Android interface and it allows to serialize a class and properties can easily transfer from one activity to another. At the end of this tutorial, you will see the following screen will be designed.

insert data from one Activity using Parcelable - Handy Opinion

In the below image you can see that data retrieve from another activity.

Retrieve data from one Activity using Parcelable - Handy Opinion

Create the Book Class

Now create a new Book class because we have to hold information of all books. This class needs to implement Parcelable and this will help to passing the data from MainActivity to BookActivity.

package com.example.myapplication.Parcelable

import android.os.Parcel
import android.os.Parcelable

class Book : Parcelable {
    // getters
    // book basics
    var title: String?
        public set
    var author: String?
        public set

    // main constructor
    constructor(title: String?, author: String?) {
        this.title = title
        this.author = author
    }

    override fun describeContents(): Int {
        return 0
    }

    // write object values to parcel for storage
    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(title)
        dest.writeString(author)
    }

    constructor(parcel: Parcel) {
        title = parcel.readString()
        author = parcel.readString()
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Book?> =
            object : Parcelable.Creator<Book?> {
                override fun createFromParcel(parcel: Parcel): Book? {
                    return Book(parcel)
                }

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

Write to the Parcel

Inside the Book.kt class, we have the writeToParcel the method that helps to add all of your class data to the package. This method allows you to add any field to the package.

override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(title)
        dest.writeString(author)
    }

Read Data From the Parcel

The write method handles the data to be transferred and the constructor is used to read the transferred data from the serialized package.

constructor(parcel: Parcel) {
        title = parcel.readString()
        author = parcel.readString()
}

Parcelable.Creator

As you can see in the above Book Model Class you need to create an instance Parcelable.Creator and assign it to the CREATOR.

companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Book?> =
            object : Parcelable.Creator<Book?> {
                override fun createFromParcel(parcel: Parcel): Book? {
                    return Book(parcel)
                }

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

Main Activity

In the MainActivity.ktfile, you need to set up an onClickListener and this function call when you press the button.

package com.example.myapplication.Parcelable

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.R

class Parcelable : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_parcelable)

        val mBkTitle = findViewById<View>(R.id.title) as EditText
        val mBkAuthor = findViewById<View>(R.id.author) as EditText

        val button =
            findViewById<View>(R.id.submit_button) as Button
        button.setOnClickListener {
            val book = Book(
                mBkTitle.text.toString(),
                mBkAuthor.text.toString()
            )
            val intent = Intent(this@Parcelable, BookActivity::class.java)
            intent.putExtra("Book", book)
            startActivity(intent)
        }
    }
}

In this tutorial, we learn how to send data from one Activity to Another using Parcelable.

If you have any questions or suggestions, feel free to ask in the comments section below.

Next Article

Send Data From one Activity to Another Using Parcelable in Java

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: