Parcelable と Bundle
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Parcelable
オブジェクトと Bundle
オブジェクトは、
IPC/Binder など、プロセスの境界を越えて使用
トランザクション、インテントとアクティビティ間のトランザクション、構成全体での一時的な状態の保存などの機能を利用できます。
できます。このページでは、Terraform の使用に関する推奨事項とベスト プラクティスについて説明します。
Parcelable
オブジェクトと Bundle
オブジェクト。
注: Parcel
は汎用ではありません。
シリアライゼーション メカニズムであるため、
Parcel
データをディスクに保存するか、ネットワーク経由で送信します。
アクティビティ間でデータを送信する
アプリが Cloud Storage で使用する Intent
オブジェクトを
新しい Activity を開始する際に 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);
OS は、インテントの基となる Bundle
をパーセル化します。その後、OS は
新しいアクティビティ
データのパーセルを解除し、インテントを新しいアクティビティに渡します。
Bundle
クラスを使用して、OS が認識しているプリミティブを設定することをおすすめします。
Intent
オブジェクト。Bundle
クラスは、
パーセルを使用したマーシャリングとアンマーシャリングに最適化されています。
場合によっては、複合オブジェクトまたは複合オブジェクトをアクティビティ間で送信するメカニズムが必要になることがあります。
このような場合、カスタムクラスで Parcelable を実装し、
writeToParcel(android.os.Parcel, int)
メソッドを使用します。
また、null ではない CREATOR
フィールドも用意する必要があります。
Parcelable.Creator
インターフェースを実装します。このインターフェース
createFromParcel()
メソッドは、Parcel
を現在のオブジェクトに戻すために使用されます。
詳しくは
Parcelable
オブジェクトのリファレンス ドキュメントをご覧ください。
インテントを介してデータを送信する場合は、データサイズを数 KB に制限するよう注意する必要があります。
送信するデータが多すぎると、システムから
TransactionTooLargeException
例外。
プロセス間でデータを送信する
プロセス間でデータを送信する方法は、アクティビティ間で行う方法と似ています。ただし、
カスタム Parcelable は使用しないことをおすすめします。カスタムの
Parcelable
オブジェクトをアプリから別のアプリに転送するには、そのアプリが
カスタムクラスとまったく同じバージョンの
送信側のアプリと受信側のアプリの両方に存在します。通常、これは共通ライブラリ
使用しています。アプリがカスタム Parcelable を
これは、システムが認識していないクラスをアンマーシャリングできないためです。
たとえば、アプリでのアラームの設定には、
AlarmManager
クラスを使用し、カスタムの Parcelable
を使用する
定義しますアラームが鳴ると、システムはインテントの
追加する特典の Bundle
繰り返し回数を表します。この変更により、システムでカスタム バックエンドが削除され、
エクストラの Parcelable
。その結果、アプリの
変更されたアラーム インテントを受け取ると、アプリがクラッシュする
存在しないデータを受信できます
Binder トランザクション バッファには、現在 1 MB の固定サイズが限られており、
処理中のトランザクションを検出できますこのプロセスでは
場合、これらのトランザクションには、すべてのバインダー トランザクションが
onSaveInstanceState、startActivity、システムとのやり取りなどのアプリに関する操作。サイズが
制限を超えると、TransactionTooLargeException がスローされます。
savedInstanceState の特定のケースでは、データの量を小さくする必要があります。
これは、提供されたデータを、ユーザーが
そのアクティビティに戻ることができる(アクティビティのプロセスが強制終了された場合でも)
保存済み状態は 5 万未満のデータに保つことをおすすめします。
注: 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."]]