kotlin uses Parcelize annotations to simplify Parcelable writing
Parcelize Note
kotlin added support for parcelable at version 1.1.4
The Android extension now includes an automated generator that implements Parcelable.The generator automatically creates the writeToParcel ()/ createFromParcel () method by declaring the serialized properties in the main constructor and adding a @Parcelize comment
Demand Environment
Kotlin version 1.1.4 or higher
Start using
stayBuild.gradleAdd support in
apply plugin: 'kotlin-android-extensions'
....
androidExtensions {
experimental = true
}
Entity Class
Student.kt
@Parcelize
data class Student(val id: String, val name: String, val grade: String) : Parcelable
Comparing Java entity classes
Student.java
public class Student implements Parcelable{
private String id;
private String name;
private String grade;
// Constructor
public Student(String id, String name, String grade){
this.id = id;
this.name = name;
this.grade = grade;
}
// Getter and setter methods
.........
.........
// Parcelling part
public Student(Parcel in){
String[] data = new String[3];
in.readStringArray(data);
// the order needs to be the same as in writeToParcel() method
this.id = data[0];
this.name = data[1];
this.grade = data[2];
}
@Оverride
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {this.id,
this.name,
this.grade});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
About Lint Check Errors
Now Android Studio will pop up this error in the image below. This is a Bug from IntelliJ. You can find the answer in the link below
https://youtrack.jetbrains.com/issue/KT-19300
So you can ignore this warning and compile the project, or you can add the @SuppressLint("ParcelCreator") comment
Links in English
https://android.jlelse.eu/yet-another-awesome-kotlin-feature-parcelize-5439718ba220