text
Official Document/guide/components/activities/parcelables-and-bundles
When sendBroadcast and startActivity, we will use Intent. Intent can carry some data, such as basic type data int, Boolean, String, or serialized objects, Parcelable and Serializable.
Exception TransactionTooLargeException
When passing data intent, if the data is too large, an exception may occur.
Notice:
In Android 7.0 (API level 24) or later, the TransactionTooLargeException is thrown at runtime. In earlier versions of Android, the system only displays warnings in logcat.
TransactionTooLargeException inherits RemoteException
package ; public class TransactionTooLargeException extends RemoteException { public TransactionTooLargeException() { super(); } public TransactionTooLargeException(String msg) { super(msg); } }
Tracking to Binder, its transactNative method will report a RemoteException
public native boolean transactNative(int code, Parcel data, Parcel reply, int flags) throws RemoteException;
Throwing an exception is related to Binder.
When sending data through intent, you should be careful to limit the data size to a few KB. Sending too much data will cause the system to throw a TransactionTooLargeException exception.
The size of the information carried by Intent is limited by Binder
The size of the information carried by Intent is actually subject to Binder restrictions. The title of this article can also be changed to "Binder pass data size limit".
Data is stored in the Binder delivery cache as a Parcel object. If the data or return value is larger than the pass buffer, the pass call fails and a TransactionTooLargeException exception is thrown.
The Binder transaction buffer has a limited size, usually1Mb. Share the cache space by all transactions being processed in the process.
Since this limitation is a process-level, not an Activity-level limitation, these transactions include all binder transactions in the application, for exampleonSaveInstanceState,startActivityas well asAny interaction with the system. TransactionTooLargeException is raised when the size limit is exceeded.
For specific cases of savedInstanceState, the amount of data should be kept at a small scale, because the system process needs to retain the provided data as long as the user can return to the Activity (even if the process of the Activity has been terminated). We recommend that you keep the saved state in50kThe following data.
Why Binder limits the size of data transmitted
Personally, I speculate that as an IPC method, Binder is not designed to transmit large amounts of data.
Alternatives
When you need to pass long strings, Bitmap, etc., don't consider using Intent to pass data
1. Single case
2、EventBus
3、Application
4. Persistent data
The above is the detailed content of solving problems when passing large amounts of data by Android Intent. For more information about passing large amounts of data by Android Intent, please pay attention to my other related articles!