Parcelable 및 번들
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Parcelable
및 Bundle
객체의 용도는
IPC/바인더와 같은 프로세스 경계에서 사용됨
인텐트가 있는 활동 간 트랜잭션, 구성 전반에 걸쳐 일시적인 상태 저장
있습니다. 이 페이지에서는 다음을 사용하기 위한 추천 및 권장사항을 제공합니다.
Parcelable
및 Bundle
객체
참고: Parcel
는 범용이 아닙니다.
직렬화 메커니즘이 필요하며
디스크에 Parcel
데이터를 저장하거나 네트워크를 통해 전송합니다.
활동 간 데이터 전송
앱에서 사용할 Intent
객체를 만드는 경우
새 활동을 시작할 때 startActivity(android.content.Intent)
앱은
매개변수를 putExtra(java.lang.String, java.lang.String)
메서드를 사용하여 축소하도록 요청합니다.
다음 코드 스니펫은 이 작업을 실행하는 방법의 예를 보여줍니다.
Kotlin
val intent = Intent(this, MyActivity::class.java).apply {
putExtra("media_id", "a1b2c3")
// ...
}
startActivity(intent)
자바
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("media_id", "a1b2c3");
// ...
startActivity(intent);
OS는 인텐트의 기본 Bundle
을 묶습니다. 그런 다음 OS는
새 활동을
데이터를 분리하고 인텐트를 새 활동에 전달합니다.
Bundle
클래스를 사용하여 OS에 알려진 프리미티브를 설정하는 것이 좋습니다.
Intent
객체. Bundle
클래스는
parcel을 사용한 마샬링 및 언마샬링에 최적화되어 있습니다.
활동 간에 합성 또는 복합 객체를 전송하는 메커니즘이 필요한 상황도 있습니다.
이러한 경우 맞춤 클래스는 Parcelable을 구현하고 적절한
writeToParcel(android.os.Parcel, int)
메서드를 사용하여 지도 가장자리에
패딩을 추가할 수 있습니다.
또한 null이 아닌 CREATOR
라는 필드도 제공해야 합니다.
Parcelable.Creator
인터페이스를 구현합니다.
<ph type="x-smartling-placeholder">createFromParcel()
</ph>
메서드는 Parcel
를 다시 현재 객체로 변환하는 데 사용됩니다.
자세한 내용은
Parcelable
객체에 관한 참조 문서를 확인하세요.
인텐트를 통해 데이터를 전송할 때 데이터 크기를 몇 KB로 제한해야 합니다.
너무 많은 데이터를 전송하면 시스템에서
TransactionTooLargeException
예외가 있습니다.
프로세스 간 데이터 전송
프로세스 간 데이터 전송은 활동 간 데이터 전송과 비슷합니다. 하지만
맞춤 parcelable을 사용하지 않는 것이 좋습니다. 맞춤 인벤토리나
Parcelable
객체를 한 앱에서 다른 앱으로 이동하는 경우
커스텀 클래스의 동일한 버전은
전송 및 수신 앱에 모두 있어야 합니다. 일반적으로 이는 일반적인 라이브러리일 수 있고
두 앱에서 모두 사용됩니다. 앱에서 맞춤 parcelable을
이는 시스템이 알지 못하는 클래스를 언마샬링할 수 없기 때문입니다.
예를 들어 앱은
AlarmManager
클래스, 맞춤 Parcelable
사용
설정합니다. 경보가 울리면 시스템이 인텐트의
Bundle
의 보너스 콘텐츠 추가
반복할 수도 있습니다 이렇게 수정하면 시스템에서 맞춤
보너스 콘텐츠의 Parcelable
입니다. 이렇게 제거하면 결과적으로 앱의
수정된 알람 인텐트를 수신할 때 비정상 종료됨
더 이상 존재하지 않는 추가 데이터를 수신합니다.
바인더 트랜잭션 버퍼의 고정 크기는 현재 1MB로 제한되며 이는 모든 사용자가 공유합니다.
진행 중인 트랜잭션이 있습니다 이 한도가 처리 중이므로
이러한 트랜잭션에는
onSaveInstanceState, startActivity 및 시스템과의 모든 상호작용 등. 크기가
한도를 초과하면 TransactionTooLargeException이 발생합니다.
savedInstanceState의 특정 사례의 경우 데이터 양을 작게 유지해야 합니다.
시스템 프로세스는 제공된 데이터를 사용자가 대기 상태로 유지하는 한
해당 Activity로 다시 이동할 수 있습니다 (활동의 프로세스가 종료되었더라도).
저장된 상태를 50k 미만의 데이터로 유지하는 것이 좋습니다.
참고: Android 7.0 (API 수준 24) 이상에서는 시스템에서
TransactionTooLargeException을 런타임 예외로 설정했습니다.
하위 버전의 Android에서는 시스템이 logcat에 경고만 표시합니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Parcelables and bundles\n\n[Parcelable](/reference/android/os/Parcelable) and [Bundle](/reference/android/os/Bundle) objects are intended to be\nused across process boundaries such as with IPC/Binder\ntransactions, between activities with intents, and to store transient state across configuration\nchanges. This page provides recommendations and best practices for using\n[Parcelable](/reference/android/os/Parcelable) and [Bundle](/reference/android/os/Bundle) objects.\n\n**Note:** [Parcel](/reference/android/os/Parcel) is not a general-purpose\nserialization mechanism, and you should never\nstore any [Parcel](/reference/android/os/Parcel) data on disk or send it over the network.\n\nSending data between activities\n-------------------------------\n\n\nWhen an app creates an [Intent](/reference/android/content/Intent) object to use in\n[startActivity(android.content.Intent)](/reference/android/app/Activity#startActivity(android.content.Intent)) in starting a new Activity,\nthe app can pass in\nparameters using the [putExtra(java.lang.String, java.lang.String)](/reference/android/content/Intent#putExtra(java.lang.String, java.lang.String))\nmethod.\n\nThe following code snippet shows an example of how to perform this operation. \n\n### Kotlin\n\n```kotlin\nval intent = Intent(this, MyActivity::class.java).apply {\n putExtra(\"media_id\", \"a1b2c3\")\n // ...\n}\nstartActivity(intent)\n```\n\n### Java\n\n```java\nIntent intent = new Intent(this, MyActivity.class);\nintent.putExtra(\"media_id\", \"a1b2c3\");\n// ...\nstartActivity(intent);\n```\n\n\nThe OS parcels the underlying [Bundle](/reference/android/os/Bundle) of the intent. Then, the OS creates\nthe new activity,\nun-parcels the data, and passes the intent to the new activity.\n\n\nWe recommend that you use the [Bundle](/reference/android/os/Bundle) class to set primitives known to the OS on\n[Intent](/reference/android/content/Intent) objects. The [Bundle](/reference/android/os/Bundle) class is highly\noptimized for marshalling and unmarshalling using parcels.\n\n\nIn some cases, you may need a mechanism to send composite or complex objects across activities.\nIn such cases, the custom class should implement Parcelable, and provide the appropriate\n[writeToParcel(android.os.Parcel, int)](/reference/android/os/Parcelable#writeToParcel(android.os.Parcel, int)) method.\nIt must also provide a non-null field called `CREATOR` that\nimplements the [Parcelable.Creator](/reference/android/os/Parcelable.Creator) interface, whose\n[createFromParcel()](/reference/android/os/Parcelable.Creator#createFromParcel(android.os.Parcel))\nmethod is used for converting the [Parcel](/reference/android/os/Parcel) back to the current object.\nFor more information,\nsee the reference documentation for the [Parcelable](/reference/android/os/Parcelable) object.\n\n\nWhen sending data via an intent, you should be careful to limit the data size to a few KB.\nSending too much data can cause the system to throw a\n[TransactionTooLargeException](/reference/android/os/TransactionTooLargeException) exception.\n\nSending data between processes\n------------------------------\n\n\nSending data between processes is similar to doing so between activities. However, when sending\nbetween processes, we recommend that you do not use custom parcelables. If you send a custom\n[Parcelable](/reference/android/os/Parcelable) object from one app to another, you need to be certain that the\nexact same version of the custom class is\npresent on both the sending and receiving apps. Typically this could be a common library\nused across both apps. An error can occur if your app tries to send a custom parcelable to\nthe system, because the system cannot unmarshal a class that it has no knowledge of.\n\n\nFor example, an app might set an alarm using\nthe [AlarmManager](/reference/android/app/AlarmManager) class, and use a custom [Parcelable](/reference/android/os/Parcelable)\non the alarm intent. When the alarm goes off, the system modifies the intent's\n[Bundle](/reference/android/os/Bundle) of extras to add\na repeat count. This modification can result in the system's stripping the custom\n[Parcelable](/reference/android/os/Parcelable) from the extras. This stripping, in turn, can result in the app's\ncrashing when it receives the modified alarm intent, because the app expects to\nreceive extra data that is no longer there.\n\n\nThe Binder transaction buffer has a limited fixed size, currently 1MB, which is shared by all\ntransactions in progress for the process. Since this limit is at the process\nlevel rather than at the per activity level, these transactions include all binder transactions in\nthe app such as onSaveInstanceState, startActivity and any interaction with the system. When the size\nlimit is exceeded, a TransactionTooLargeException is thrown.\n\n\nFor the specific case of savedInstanceState, the amount of data should be kept small\nbecause the system process needs to hold on to the provided data for as long as the user\ncan ever navigate back to that activity (even if the activity's process is killed).\nWe recommend that you keep saved state to less than 50k of data.\n\n\n**Note:** In Android 7.0 (API level 24) and higher, the system throws a\nTransactionTooLargeException as a runtime exception.\nIn lower versions of Android, the system only shows a warning in logcat."]]