Why does Android N throw TransactionTooLargeException when using Bundles? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-11 01:44:54

问题


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 rethrows TransactionTooLargeExceptions as RuntimeExceptions, instead of silently logging or suppressing them. One common example is storing too much data in Activity.onSaveInstanceState(), which causes ActivityThread.StopInfo to throw a RuntimeException when your app targets Android 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.

  1. If it is binary data or a bitmap, it is best to store it in a file and pass the path in the bundle.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!