Both Kotlin and Java implement Serializable or Parcelable interfaces. The two languages implement Serializable or Parcelable interfaces differently. The following is a simple example of how Serializable can be implemented:
Parcelable in Java
Example code:
public class Test implements Parcelable {
private String name;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
protected Test(Parcel in) {
name=in.readString();
}
public static final Creator<Test> CREATOR = new Creator<Test>() {
@Override
public Test createFromParcel(Parcel in) {
return new Test(in);
}
@Override
public Test[] newArray(int size) {
return newTest[size]; }};@Override
public int describeContents(a) {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}
Copy the code
Focus on a few key points:
- The Test constructor. Do not use the default no-argument constructor. When using Parcelable serialization, the IDE prompts you to create a constructor that takes Parcel, so add it as prompted
- Need to override Parcelable’s two methods describeContents and writeToParcel
- CREATOR is not added by default, just add it as prompted by the IDE. It is important to note that the generic type in CREATOR generics must be consistent with the current class
2 Parcelable in Kotlin
There are two ways to implement serialization using Parcelable in Kotlin, either in a Java-like way or using annotations. Let’s start with the traditional java-like approach:
The traditional way
class TestKT() :Parcelable{
vartest:String? =null
constructor(parcel: Parcel) : this() {
test = parcel.readString()
}
override fun describeContents(a): Int {
TODO("Not yet implemented")}override fun writeToParcel(dest: Parcel? , flags:Int) {
TODO("Not yet implemented")}companion object CREATOR : Parcelable.Creator<TestKT> {
override fun createFromParcel(parcel: Parcel): TestKT {
return TestKT(parcel)
}
override fun newArray(size: Int): Array<TestKT? > {return arrayOfNulls(size)
}
}
}
Copy the code
The implementation is the same as Java, except that the language and syntax are kotlin.
Here’s how to do this using annotations:
Annotation way
import kotlinx.parcelize.Parcelize
@Parcelize
class TestKT() : Parcelable {
vartest:String? =null
}
Copy the code
This uses the Parcelize annotation, which was packaged into the Kotlin Android Extensions plug-in in the early versions of Kotlin. This plugin has been deprecated and the serialization function has been migrated to another plugin, kotlin-parcelize, so if we have a newer version of Kotlin, You can use parcelize annotations by importing the kotlin-parcelize plugin directly into your app’s build file (import the kotlinx.parcelize package).
3 Serializable in Java
Java simply implements the Serializable interface without overriding any methods because Serializable is an empty interface
public class Test implements Serializable {
private String name;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}Copy the code
4 Serializable in Kotlin
Like Java, no annotations are required
class TestKT() : Serializable {
vartest:String? =null
}
Copy the code
So that’s a personal summary of the difference between Java and Kotlin implementations of serialization