2

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?

Tim
  • 41,901
  • 18
  • 127
  • 145
JAAD
  • 12,349
  • 7
  • 36
  • 57
  • @VivekMishra this tries to solve the question not just state why it happens so not a duplicate – JAAD Sep 15 '16 at 09:23
  • To add to the already existing answer i am posting a link to the blog that has a sample code http://blog.sqisland.com/2016/09/transactiontoolargeexception-crashes-nougat.html – Raghunandan Sep 30 '16 at 15:51
  • @Raghunandan you are free to edit the answer and add that link – JAAD Sep 30 '16 at 17:57
  • @ColdFire its just an link. Its better to post the content of the blog as the link might die in the future. – Raghunandan Sep 30 '16 at 17:59
  • @Raghunandan yes i mean you are free to add that content too – JAAD Sep 30 '16 at 18:00
  • @ColdFire its better you do it cause some people reviewing might reject the same( if they don't understand the edit. they might think its not relevant). I am not sure that depends on the rep though – Raghunandan Sep 30 '16 at 18:06

1 Answers1

15

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.

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
JAAD
  • 12,349
  • 7
  • 36
  • 57