พาร์เซลและกลุ่ม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ออบเจ็กต์ Parcelable
และ Bundle
ควรเป็น
ใช้ข้ามขอบเขตของกระบวนการ เช่น กับ IPC/Binder
ธุรกรรม ระหว่างกิจกรรมที่มี Intent และเพื่อจัดเก็บสถานะชั่วคราวในการกำหนดค่า
การเปลี่ยนแปลง หน้านี้จะให้คำแนะนำและแนวทางปฏิบัติแนะนำในการใช้
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)
Java
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("media_id", "a1b2c3");
// ...
startActivity(intent);
ระบบปฏิบัติการอัปโหลด Bundle
เบื้องหลังของ Intent จากนั้น ระบบปฏิบัติการจะสร้าง
กิจกรรมใหม่
ยกเลิกการจัดกลุ่มข้อมูล และส่งต่อความตั้งใจไปยังกิจกรรมใหม่
เราขอแนะนำให้คุณใช้คลาส Bundle
เพื่อตั้งค่าพื้นฐานที่ทราบของระบบปฏิบัติการ
Intent
ออบเจ็กต์ ชั้นเรียน Bundle
มีราคาสูงมาก
ซึ่งใช้แปลงที่ดิน
ในบางกรณี คุณอาจต้องการกลไกในการส่งออบเจ็กต์แบบผสมหรือซับซ้อนไปยังกิจกรรมต่างๆ
ในกรณีเช่นนี้ คลาสที่กำหนดเองควรใช้ Parcelable และระบุ
writeToParcel(android.os.Parcel, int)
วิธี
และต้องมีช่องที่ไม่เป็นค่าว่างที่เรียกว่า CREATOR
ซึ่ง
จะใช้อินเทอร์เฟซ Parcelable.Creator
ซึ่ง
createFromParcel()
ใช้เพื่อแปลง Parcel
กลับเป็นออบเจ็กต์ปัจจุบัน
สำหรับข้อมูลเพิ่มเติม
ดูเอกสารอ้างอิงสำหรับออบเจ็กต์ Parcelable
เมื่อส่งข้อมูลผ่าน Intent คุณควรจำกัดขนาดของข้อมูลให้ไม่เกิน 2-3 KB
การส่งข้อมูลมากเกินไปอาจทำให้ระบบส่ง
ข้อยกเว้น TransactionTooLargeException
รายการ
การส่งข้อมูลระหว่างกระบวนการ
การส่งข้อมูลระหว่างขั้นตอนจะคล้ายกับการส่งข้อมูลระหว่างกิจกรรม แต่เมื่อส่ง
ขอแนะนำว่าอย่าใช้พัสดุไปรษณีย์ที่กำหนดเองในระหว่างกระบวนการ หากคุณส่งฟิลด์ที่กำหนดเอง
Parcelable
จากแอปหนึ่งไปยังอีกแอปหนึ่ง คุณต้องตรวจสอบว่า
คลาสที่กำหนดเองในเวอร์ชันเดียวกันคือ
ที่ปรากฏทั้งในแอปการส่งและการรับ โดยทั่วไปแล้วจะเป็นไลบรารีที่ใช้ร่วมกัน
ซึ่งใช้ในทั้ง 2 แอป ข้อผิดพลาดอาจเกิดขึ้นหากแอปของคุณพยายามส่งพัสดุที่กำหนดเองไปยัง
เนื่องจากระบบไม่อาจปลดเปลื้องชั้นซึ่งระบบไม่มีความรู้มาก่อน
ตัวอย่างเช่น แอปอาจตั้งปลุกโดยใช้
AlarmManager
และใช้ Parcelable
ที่กำหนดเอง
เกี่ยวกับความตั้งใจในการปลุก เมื่อสัญญาณเตือนดังขึ้น ระบบจะปรับเปลี่ยน Intent
มีรายการเพิ่มเติมอีก Bundle
รายการ
การทำซ้ำ การแก้ไขนี้อาจส่งผลให้ระบบตัดการกำหนดค่า
Parcelable
จากรายการเพิ่มเติม การกำจัดนี้จะส่งผลให้แอปแสดง
ขัดข้องเมื่อได้รับ Intent ของการปลุกที่แก้ไขแล้ว เนื่องจากแอปคาดว่า
จะได้รับข้อมูลเพิ่มเติมที่ไม่มีอีกต่อไปแล้ว
บัฟเฟอร์ธุรกรรม Binder มีขนาดคงที่ที่จำกัด ซึ่งปัจจุบันมีขนาด 1 MB ซึ่งแชร์โดยทุกคน
ธุรกรรมที่อยู่ระหว่างดำเนินการ เนื่องจากขีดจำกัดนี้อยู่ในกระบวนการ
แทนที่จะเป็นระดับตามกิจกรรม ธุรกรรมเหล่านี้จะรวมธุรกรรม Binder ทั้งหมดไว้ใน
แอป เช่น onSaveInstanceState, startActivity และการโต้ตอบกับระบบ เมื่อขนาด
เกินขีดจำกัดแล้ว ระบบส่ง TransactionTooLargeException
ในกรณีของ savedInstanceState ให้เก็บข้อมูลไว้จำนวนน้อย
เนื่องจากระบบจำเป็นต้องเก็บข้อมูลที่ให้ไว้ไว้ตราบเท่าที่ผู้ใช้
จะสามารถกลับไปยังกิจกรรมนั้นได้ (แม้ว่ากระบวนการของกิจกรรมจะหายไปก็ตาม)
เราขอแนะนำให้คุณคงสถานะที่บันทึกไว้ให้มีข้อมูลน้อยกว่า 50k
หมายเหตุ: ใน Android 7.0 (API ระดับ 24) ขึ้นไป ระบบจะแสดงข้อผิดพลาด
TransactionTooLargeException เป็นข้อยกเว้นรันไทม์
ใน Android เวอร์ชันต่ำกว่า ระบบจะแสดงคำเตือนใน Logcat เท่านั้น
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ 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."]]