<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Khushbu Shah on Medium]]></title>
        <description><![CDATA[Stories by Khushbu Shah on Medium]]></description>
        <link>https://medium.com/@khushbu.shah2302?source=rss-12abd5b59827------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qUb1C9NjXaSvqsWf.jpg</url>
            <title>Stories by Khushbu Shah on Medium</title>
            <link>https://medium.com/@khushbu.shah2302?source=rss-12abd5b59827------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:54:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khushbu.shah2302/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Parcelable Data Classes in Kotlin]]></title>
            <link>https://medium.com/@khushbu.shah2302/understanding-parcelable-data-classes-in-kotlin-1390e483500e?source=rss-12abd5b59827------2</link>
            <guid isPermaLink="false">https://medium.com/p/1390e483500e</guid>
            <category><![CDATA[data-class]]></category>
            <category><![CDATA[kotlin-beginners]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[parcelable]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Khushbu Shah]]></dc:creator>
            <pubDate>Mon, 11 Aug 2025 01:34:15 GMT</pubDate>
            <atom:updated>2025-08-11T01:34:15.118Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C6PiKuwXsKe0A4mWeTi4jQ.jpeg" /></figure><blockquote>“Kotlin makes it simple to work with <strong>Parcelable data classes</strong> using the <strong>@Parcelize</strong> annotation. In this blog, I’ll share how to make any data class Parcelable in Kotlin, along with a peek under the hood to see exactly how @Parcelize works.”</blockquote><p>In this blog, I will explain how to make Parcelable data class in Kotlin in very easy way and how it works internally.</p><p>When I began converting my Android app code from Java to Kotlin, I started with a simple yet powerful concept: the data class.</p><p>In any application, we often need classes to hold actual data — commonly referred to as model or POJO classes in Java. In Kotlin, we use <strong>data classes</strong> to serve this purpose in a cleaner and more concise way.</p><p>While converting these data holder classes, I needed them to be Parcelable—so that existing app functionality relying on inter-component data passing would remain intact.</p><p>In this blog, I’ll walk you through:</p><ul><li>How to make a data class Parcelable in Kotlin</li><li>What happens under the hood (the actual generated code)</li></ul><h4>What is a Data Class in Kotlin?</h4><p>A <strong>data class</strong> in Kotlin is a special class designed to hold data. It significantly reduces boilerplate code by auto-generating functions like equals(), hashCode(), toString(), copy(), and componentN() based on the properties we define in the primary constructor.</p><pre>data class Book(val title: String, val author: String, val publicationYear: Int)</pre><h4>Why Parcelable?</h4><p>Almost all model classes in my Android app are Parcelable, because it&#39;s the recommended way to pass custom data between components like Activity, Fragment, or even through Intents and Bundles.</p><p>Compared to Java serialization, Parcelable is more efficient for Android-specific use cases.</p><h4>Making a Data Class Parcelable in Kotlin</h4><p>In Kotlin, we can easily make a data class Parcelable using the <strong>kotlin-parcelize plugin</strong> and the <strong>@Parcelize</strong> annotation—no more manually implementing boilerplate methods!</p><p>Step 1: Add the plugin to your app-level build.gradle</p><pre>plugins {<br>    id &#39;com.android.application&#39;<br>    id &#39;kotlin-android&#39;<br>    id &#39;kotlin-parcelize&#39; // Add this line<br>}</pre><p>Step 2: Annotate your data class and extend class with Parcelable</p><pre>import android.os.Parcelable<br>import kotlinx.parcelize.Parcelize<br><br>@Parcelize // Add this annotation <br>data class Book(val title: String, val author: String, val publicationYear: Int) : Parcelable</pre><p>That’s it! No need to write writeToParcel(), describeContents(), or CREATOR.</p><h4>Decompiled Code: Behind the Scenes</h4><p>Here’s what the Kotlin compiler generates when you use @Parcelize:</p><pre>@Parcelize<br>public final class Book implements Parcelable {<br>   @NotNull<br>   private final String title;<br>   @NotNull<br>   private final String author;<br>   private final int publicationYear;<br>   @NotNull<br>   public static final Parcelable.Creator CREATOR = new Creator();<br><br>   public Book(@NotNull String title, @NotNull String author, int publicationYear) {<br>      Intrinsics.checkNotNullParameter(title, &quot;title&quot;);<br>      Intrinsics.checkNotNullParameter(author, &quot;author&quot;);<br>      super();<br>      this.title = title;<br>      this.author = author;<br>      this.publicationYear = publicationYear;<br>   }<br><br>   @NotNull<br>   public final String getTitle() {<br>      return this.title;<br>   }<br><br>   @NotNull<br>   public final String getAuthor() {<br>      return this.author;<br>   }<br><br>   public final int getPublicationYear() {<br>      return this.publicationYear;<br>   }<br><br>   @NotNull<br>   public final String component1() {<br>      return this.title;<br>   }<br><br>   @NotNull<br>   public final String component2() {<br>      return this.author;<br>   }<br><br>   public final int component3() {<br>      return this.publicationYear;<br>   }<br><br>  @NotNull<br>   public final Book copy(@NotNull String title, @NotNull String author, int publicationYear) {<br>      Intrinsics.checkNotNullParameter(title, &quot;title&quot;);<br>      Intrinsics.checkNotNullParameter(author, &quot;author&quot;);<br>      return new Book(title, author, publicationYear);<br>   }<br><br>   // $FF: synthetic method<br>   public static Book copy$default(Book var0, String var1, String var2, int var3, int var4, Object var5) {<br>      if ((var4 &amp; 1) != 0) {<br>         var1 = var0.title;<br>      }<br><br>      if ((var4 &amp; 2) != 0) {<br>         var2 = var0.author;<br>      }<br><br>      if ((var4 &amp; 4) != 0) {<br>         var3 = var0.publicationYear;<br>      }<br><br>      return var0.copy(var1, var2, var3);<br>   }<br><br>   @NotNull<br>   public String toString() {<br>      return &quot;Book(title=&quot; + this.title + &quot;, author=&quot; + this.author + &quot;, publicationYear=&quot; + this.publicationYear + &#39;)&#39;;<br>   }<br><br>   public int hashCode() {<br>      int result = this.title.hashCode();<br>      result = result * 31 + this.author.hashCode();<br>      result = result * 31 + Integer.hashCode(this.publicationYear);<br>      return result;<br>   }<br><br>   public boolean equals(@Nullable Object other) {<br>      if (this == other) {<br>         return true;<br>      } else if (!(other instanceof Book)) {<br>         return false;<br>      } else {<br>         Book var2 = (Book)other;<br>         if (!Intrinsics.areEqual(this.title, var2.title)) {<br>            return false;<br>         } else if (!Intrinsics.areEqual(this.author, var2.author)) {<br>            return false;<br>         } else {<br>            return this.publicationYear == var2.publicationYear;<br>         }<br>      }<br>   }<br><br>   public int describeContents() {<br>      return 0;<br>   }<br><br>   public void writeToParcel(@NotNull Parcel out, int flags) {<br>      Intrinsics.checkNotNullParameter(out, &quot;out&quot;);<br>      out.writeString(this.title);<br>      out.writeString(this.author);<br>      out.writeInt(this.publicationYear);<br>   }<br><br>   @Metadata(<br>      mv = {2, 0, 0},<br>      k = 3,<br>      xi = 48<br>   )<br><br>   public static final class Creator implements Parcelable.Creator {<br>      public final Book[] newArray(int size) {<br>         return new Book[size];<br>      }<br><br>      public final Book createFromParcel(Parcel parcel) {<br>         Intrinsics.checkNotNullParameter(parcel, &quot;parcel&quot;);<br>         return new Book(parcel.readString(), parcel.readString(), parcel.readInt());<br>      }<br><br>      // $FF: synthetic method<br>      // $FF: bridge method<br>      public Object[] newArray(int size) {<br>         return this.newArray(size);<br>      }<br><br>      // $FF: synthetic method<br>      // $FF: bridge method<br>      public Object createFromParcel(Parcel source) {<br>         return this.createFromParcel(source);<br>      }<br>   }<br>}</pre><p>Notice how everything — CREATOR, writeToParcel(), describeContents()—is automatically handled. This saves a lot of development time and reduces errors.</p><h4>Benefits of Using @Parcelize</h4><ul><li><strong>No boilerplate</strong> — you avoid manually writing tedious methods.</li><li><strong>Cleaner and safer code</strong> — less room for errors.</li><li><strong>Auto-updates</strong> — any change in data class properties reflects in the generated code.</li><li><strong>Better performance</strong> — compared to Serializable in Android.</li></ul><h4>Final Thoughts</h4><p>Kotlin’s @Parcelize makes Android development smoother by abstracting away the repetitive Parcelable implementation. If you&#39;re converting your Java app to Kotlin or starting fresh, using @Parcelize for your model classes is a no-brainer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1390e483500e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>