LoginSignup
7
3

More than 5 years have passed since last update.

Kotlinの@Parcelizeを使ってみた

Posted at

Parcelizeとは

Kotlin 1.1.4から提供された実験的な機能

詳しくは公式ドキュメントを参照
https://kotlinlang.org/docs/tutorials/android-plugin.html#parcelable

何が便利なのか

簡単に書くと、Parcelable型の長いコードが@Parcelizeアノテーションによって劇的に短くなる!

導入方法

公式にも載っているとおり実験フラグをオンにする

app/build.gradle
     androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
 }
+
+androidExtensions {
+    experimental = true
+}

あとはParcelableクラスにアノテーションをつけるだけ

+@Parcelize
data class Animal(val dog: String, val cat: String, val bird: String) : Parcelable

//Parcelableに必要なコードは自動生成される

注意する点は、Parcelableを継承する必要があること

参考リンク

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3