Copy the data in the class and object. We use the attribute @Parcelize and the interface Parcelable. | Kotlin | Android Studio
last updated: 2 January 2024
interface Parcelable is used to copy an object, nested objects.
Where used in Android : interface Parcelable is used in Android in Intent to pass our object to Activity .
Introduction. What methods does interface Parcelable contain?
Let's take a look in the Java Android library:
1)
writeToParcel (@NonNull Parcel var1, int var2)
to pack our object in Parcel
2)
T createFromParcel (Parcel var1)
to create a copy of our object from Parcel
Step 1. Creating a new project
Step 2. Let's change the code in the file MainActivity.kt
The color indicates that a new code has been added.
Kotlin
File MainActivity.kt
package com.example.androidkotlinapp1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable // interface Parcelable defined in standard Java
import kotlinx.android.parcel.Parcelize // attribute @Parcelize defined in Kotlin extension
// 'org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.7.0'
@Parcelize
data class MyBook (
val id: Int,
val title: String ?,
val price: Int
) : Parcelable
class MainActivity : AppCompatActivity() {
override fun onCreate (savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val book1 = MyBook(id=3, title = "Hello" , price = 20)
val parcel = Parcel.obtain()
book1.writeToParcel(parcel, 0)
parcel.setDataPosition(0)
val book1FromParcel = MyHelper().MyCreator<MyBook>().createFromParcel(parcel)
}
}
class MyHelper
{
inline fun <reified T : Parcelable> MyCreator(): Parcelable.Creator<T>
{
return T::class.java.getDeclaredField("CREATOR" ).get(null) as Parcelable.Creator<T>
}
}
Why do we need the @Parcelize attribute?
Answer: The @Parcelize attribute to my MyBook class adds methods:
• writeToParcel
• createFromParcel
• describeContents
Step 3. Let's change the code in the file build.gradle
My added code is filled in:
File build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
android {
namespace 'com.example.androidkotlinapp1'
compileSdk 33
buildFeatures {
viewBinding true
}
defaultConfig {
applicationId "com.example.androidkotlinapp1"
minSdk 27
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt' ), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// kotlin (parceable)
implementation 'org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.7.0'
}
Before compiling the project, press Sync Now :
Step 4. Let's run the project in debugging
To run a Kotlin Android project in debugging in Android Studio ...
We can see that the new object
book1FromParcel has been successfully copied from the object
book1
Download an example: