Activity간에 Data를 묶어서 전달해야 할 경우가 있다.
자바로 구현한 예제를 코틀린으로 변환하면 일단 에러가 난다.
import android.os.Parcel; |
Android Studio가 제공하는 파일 변환 기능으로 코틀린으로 변환하고 약간 수작업으로 수정한 예제
import android.os.Parcel |
그런데 이런 변환 과정을 할 필요없이 아주 간단하게 해결할 수 있다.
https://developer.android.com/kotlin/parcelize 를 참조하자.
앱 build.gradle 추가사항
androidExtensions { |
@Parcelize annotation을 사용하면, writeToParcel()/createFromParcel()를 자동으로 구현해준다.
아래 코드를 보라. 얼마나 간단한가.
import android.os.Parcelable |
Activity 이동전
val intent = Intent(this@MainActivity, TargetActivity::class.java) |
Activity 이동후
val profile: Profiles = intent.getParcelableExtra("parcel") |
'안드로이드 > Kotlin 기능' 카테고리의 다른 글
[코틀린] Retrofit2 데이터 처리 파헤치기 (0) | 2021.01.22 |
---|---|
[코틀린] SQLite (0) | 2021.01.17 |
[자바-코틀린] Intent, context (0) | 2020.10.22 |
[코틀린] Java static 메서드를 코틀린으로 변환시 수정 작업 (0) | 2020.10.22 |
[코틀린] HttpURLConnection 통신 예제 (0) | 2020.06.24 |