Practice first posted on the official document developer. The android. Google. Cn/guide/compo…
In sendBroadcast, startActivity, we use Intent. Intents can carry data such as basic types int, Boolean, String, or serialized objects, Parcelable, and Serializable.
The Intent to transfer data, if the data is too big, may appear abnormal TransactionTooLargeException.
Note: in Android 7.0 (API level 24) or higher version, the system will throw TransactionTooLargeException anomalies at runtime. On earlier versions of Android, the system only displays warnings in logcat.
TransactionTooLargeException inherited remoteexceptions
package android.os; public class TransactionTooLargeException extends RemoteException { public TransactionTooLargeException() { super(); } public TransactionTooLargeException(String msg) { super(msg); }}Copy the code
Traced to Binder, whose transactNative method reports a RemoteException
public native boolean transactNative(int code, Parcel data, Parcel reply,
int flags) throws RemoteException;
Copy the code
Throwing exceptions is related to Binder.
When sending data through intEnts, be careful to limit the data size to a few kilobytes. Send too much data may lead to throw TransactionTooLargeException system anomalies.
The size of information carried by intEnts is limited by binders
The size of information that intEnts can carry is limited by binders. The title of this article could have been “Binder Transfer Data Size Limits.”
The data is stored in a Binder delivery cache as Parcel objects. If the data or the return value is greater than transmit buffer, then pass the call fails and throw TransactionTooLargeException anomalies.
Binder transaction buffers have a limited size, usually 1Mb. The cache space is shared by all transactions being processed in the process.
Because this restriction is process-level and not activity-level, these transactions include all binder transactions in the application, such as onSaveInstanceState, startActivity, and any interactions with the system. More than one size limit, will trigger a TransactionTooLargeException.
In the case of savedInstanceState, keep the amount of data small because the system process needs to retain the data provided as long as the user can return to the Activity (even if the Activity’s process has been terminated). We recommend that you keep the saved state below 50K data.
Why do binders limit the size of data transferred
As a means of IPC, Binder was not designed to transmit large amounts of data.
alternative
When passing long strings, bitmaps, etc., don’t consider using intents to pass data
1, the singleton
2, EventBus
3, the Application
4. Persist data
By AnRFDev link: juejin.cn/post/684490… Source: Rare earth mining copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.