本文摘自——https://kotlinlang.org/docs/tutorials/android-plugin.html

Android Extensions plugin provides Parcelable implementation generator as an experimental feature. To be able to use it, turn on the experimental flag.

How to use

Annotate the class with @Parcelize, and a Parcelable implementation will be generated automatically.

import kotlinx.android.parcel.Parcelize
@Parcelize
class User(val firstName: String, val lastName: String, val age: Int): Parcelable

@Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.

If your class requires more advanced serialization logic, you can write it inside a companion class:

@Parcelize
data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
private companion object : Parceler<User> {
override fun User.write(parcel: Parcel, flags: Int) {
// Custom write implementation
} override fun create(parcel: Parcel): User {
// Custom read implementation
}
}
}

Supported types

@Parcelize supports a wide range of types:

  • primitive types (and their boxed versions);
  • objects and enums;
  • StringCharSequence;
  • Exception;
  • SizeSizeFBundleIBinderIInterfaceFileDescriptor;
  • SparseArraySparseIntArraySparseLongArraySparseBooleanArray;
  • all Serializable (yes, Date is supported too) and Parcelable implementations;
  • collections of all supported types: List (mapped to ArrayList), Set (mapped to LinkedHashSet), Map (mapped to LinkedHashMap);
    • Also a number of concrete implementations: ArrayListLinkedListSortedSetNavigableSetHashSetLinkedHashSetTreeSetSortedMapNavigableMapHashMapLinkedHashMapTreeMapConcurrentHashMap;
  • arrays of all supported types;
  • nullable versions of all supported types.

Custom Parcelers

Even if your type is not supported directly, you can write a Parceler mapping object for it.

class ExternalClass(val value: Int)

object ExternalClassParceler : Parceler<ExternalClass> {
override fun create(parcel: Parcel) = ExternalClass(parcel.readInt()) override fun ExternalClass.write(parcel: Parcel, flags: Int) {
parcel.writeInt(value)
}
}

External parcelers can be applied using @TypeParceler or @WriteWith annotations:

// Class-local parceler
@Parcelize
@TypeParceler<ExternalClass, ExternalClassParceler>()
class MyClass(val external: ExternalClass) // Property-local parceler
@Parcelize
class MyClass(@TypeParceler<ExternalClass, ExternalClassParceler>() val external: ExternalClass) // Type-local parceler
@Parcelize
class MyClass(val external: @WriteWith<ExternalClassParceler>() ExternalClass)
 

最新文章

  1. 循序渐进做项目系列(5):制作安装包,谁人都可以!——VS制作安装包简明教程
  2. HTTPS那些事(一)HTTPS原理(转载)
  3. https 页面中引入 http 资源的解决方式
  4. Win10系统Start Menu上的图标莫名消失
  5. JavaScript开发规范要求
  6. OpenGL的gluPerspective和gluLookAt的关系[转]
  7. springMVC导出 CSV案例
  8. JavaScript之放大镜效果
  9. C++四种强制类型转换关键字
  10. (极简)linux安装QT4.7.3
  11. 隐藏AutoCompleteTextView下拉框的滚动条
  12. RealThinClient学习(一)
  13. 1.1 selenium 安装
  14. 20170310 - Python 3 下 SQLAlchemy 的 MySQL 数据库 URI 配置
  15. ROS-十步完成ROS-indigo安装
  16. CMD管道命令使用
  17. 使用android访问SQLServer数据库
  18. webGL之three.js入门3--材料篇
  19. LCTF wp简单复现
  20. Python入门编程中的变量、字符串以及数据类型

热门文章

  1. k8s-创建自定义chart及部署efk-二十五
  2. 任务45:Identity MVC:注册逻辑实现
  3. NOIP2017 赛后总结
  4. 黑客攻防技术宝典web实战篇:攻击用户&#183;其他技巧习题
  5. 一起学Android之Activity
  6. 通过split命令分割大文件
  7. python中的栈
  8. 模拟 2015百度之星资格赛 1003 IP聚合
  9. D. The Door Problem 带权并查集
  10. HBase文档操作--练习篇