Android

Android implement Kotlin Parcelable

1.創建一個Modle,實現Parcelable並在類前面添加@Parcelize註釋

@Parcelize
data class Student(val name: String, val age: Int) : Parcelable

2.利用intent傳值到另一個頁面

val tomStudent = Student("Tom", 15)
val tonyStudnet = Student("Tony", 11)
val amyStudent = Student("Amy", 17)

val intent = Intent(this, TwoActivity::class.java)
intent.putExtra("students", arrayListOf(tomStudent, amyStudent, tonyStudnet))
startActivity(intent)

3.另一個頁面取得Model物件

val students = intent.getParcelableArrayListExtra<Student>("students")

students?.run {
     indices.forEach {
         Log.d("name", students[it].name)
         Log.d("age", students[it].age.toString())
     }
}

發表迴響