问题
On Android N whenever I pass some binary or large data in bundle I get a TransactionTooLargeException
, however it runs without issues on android M and below.
How can I solve this?
回答1:
There has been a behavior change in Android N
Quoting the docs:
Many platform APIs have now started checking for large payloads being sent across
Binder
transactions, and the system now rethrowsTransactionTooLargeExceptions
asRuntimeExceptions
, instead of silently logging or suppressing them. One common example is storing too much data inActivity.onSaveInstanceState()
, which causesActivityThread.StopInfo
to throw aRuntimeException
when your app targetsAndroid 7.0
.
Note: Apps targeting M or below won't throw the exception, they will just silently log or suppress them
How to solve this:
Rethink why you need so much data in bundle in the first place.
If it is binary data or a bitmap, it is best to store it in a file and pass the path in the bundle.
If you are passing too many objects
You can use libraries like
Otto
,EventBus
to avoid it.Just pass the necessary info required for constructing the Object once again.
Create a singleton class and set the data there and access it in another Activity or Fragment from there.
来源:https://stackoverflow.com/questions/39507250/why-does-android-n-throw-transactiontoolargeexception-when-using-bundles