本文摘自——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)
 

kotlin - Parcelable implementations generator的更多相关文章

  1. Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator

    Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Gen ...

  2. Android Studio 优秀插件: Parcelable Code Generator

    这里假设我们已经会使用 Parcelable 序列化一个对象了~~ 那么大家会发现 Parcelable 使用起来有些复杂,因为我们要自己复写 几个方法,而且当类的属性比较多的时候,我们就会难受了,又 ...

  3. Android Studio酷炫插件(一)——自动化快速实现Parcelable接口序列化

    https://blog.csdn.net/kroclin/article/details/40902721 一.前言相信数据序列化大家都多多少少有接触到,比如自定义了一个实体类,需要在activit ...

  4. 服务 AIDL 定向tag IPC Parcelable 案例 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Android Studio自动化快速实现Parcelable接口序列化

    1.在线安装 然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入android parcelable code g ...

  6. Android Studio各种快捷功能及好用的插件

    转载:http://www.jianshu.com/p/c439605a4364 概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的, ...

  7. C++ 下面的AIDL

    转自https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md. Bac ...

  8. Android Weekly Notes Issue #257

    Android Weekly Issue #257 May 14th, 2017 Android Weekly Issue #257 本期内容包括: Gradle中关于项目的一些设置; Android ...

  9. [Android Tips] 19. Android Studio Plugins

    Code Generation GsonFormat json 字符串生成实体类 https://github.com/zzz40500/GsonFormat Android Parcelable C ...

随机推荐

  1. DC 兼容的DC

    DC是 "Device Content" , MS VC++ 的 MFC图形设备接口 的 设备描述表.它是MFC的主要对象之一.通过CDC类进行各种绘图操作,例如选笔,选色,选涂色 ...

  2. 6-10 SVM支持向量机1

    都是特征加上分类器.还将为大家介绍如何对这个数据进行训练.如何训练得到这样一组数据. 其实SVM支持向量机,它的本质仍然是一个分类器.既然是一个分类器,它就具有分类的功能.我们可以使用一条直线来完成分 ...

  3. mysql5.7根据.frm和.ibd文件恢复表结构和数据

    一.恢复表结构 1.环境:Windows .mysql5.7:首先创建一个数据库,可以通过navicat来创建:     2.使用当前创建的数据库:use ww; 3.随意创建一张表,但是这张表的名字 ...

  4. ubuntu设置里面怎么少了好多设置了比如桌面背景

    哈哈  我也是醉了  这个虚拟机真的不好对付 解决办法:sudo apt-get install unity-control-center          ok!

  5. JAVA基础--异常10

    一.Object类简介 1.Object类简介 Object,是Java一切对象的最高父类:如果一个类没有显示继承一个类,他的父类就是Object: 它描述的是Java世界里所有对象最最共性的信息,它 ...

  6. H5+JS实现手机摇一摇功能

    在做微信活动页面的时候,经常会需要实现手机摇一摇功能,比如“摇一摇,拿好礼”. 为了实现它,就需要用到HTML5的DeviceOrientation特性.它提供的DeviceMotion事件封装了设备 ...

  7. 转载文章 -- 难搞的滚动事件(滚动默认,scrollTop)

    关于取消默认事件 现今的 chrome 浏览器,为了实现丝滑顺畅地滑动,活动时间直接执行而不再检测默认事件,这使得无法用 e.preventDafult() 来阻止默认事件. 现在需要添加 {pass ...

  8. Spring Security 使用Ajax登陆无法跳转页面解决方法

    使用Security的朋友都知道,使用Security后,不再需要我们自己过多的(还需要写少量代码)写登陆的逻辑,只需要自己在html的登陆表单上面定义好输入框name为:username和passw ...

  9. 编译boost asio http/server 方法

    这段时间学习boost 的asio 编程,想编译asio自带的http/server的程序,无奈在网上根本找不到方法,只能自己摸索学习. 登陆boost asio 的example 目录,(我 boo ...

  10. AtCoder Grand Contest 003 E - Sequential operations on Sequence

    题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_e 题目大意 一串数,初始为\(1\sim N\),现有\(Q\)个操作,每次操作会把数组长度 ...