TransactionTooLargeException

Lifecycle and app components Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

The app crashes when users switch away from a gallery screen that saves 500 thumbnails in onSaveInstanceState. Best fix?

Options
Hint 1 of 1

What is the smallest data that can rebuild this screen?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Save only the IDs and scroll position, and reload thumbnails from the cache

Correct. The saved Bundle must be small; data belongs on disk.

Why not the others?
  • B Compress the thumbnails before saving. Still large, and still shares the Binder buffer.
  • C Catch TransactionTooLargeException. It's thrown by the system on the way out; you can't reliably catch it.
  • D Save them in a static field. That won't survive process death, which is why state is saved in the first place.

30-second answer

Intents, saved instance state and other cross-process calls go through Binder, whose transaction buffer is about 1 MB shared by all in-flight transactions in the process. Putting large data in an Intent extra or a saved Bundle, such as a bitmap or a long list, can exceed it, and the app crashes, often when the Activity stops and saves state. Pass IDs and load data from disk or a repository.

Practise saying it · 30 s. No recording, just a timer

Explain TransactionTooLargeException.

Say your answer out loud, then check which of these points you covered:

  • Binder buffer about 1 MB, shared
  • Large extras or saved state cause it
  • Pass IDs, not data
  • Shows up on stop

Deep dive

Keep individual saved states small; Google suggests well under 50 KB.

Fragments' saved state and Navigation arguments go into the same Bundle, so they add up.

Tools such as TooLargeTool can log Bundle sizes in debug builds.

Bitmaps should be shared through a file or cache with a URI, not as extras.

Common mistakes

  • Passing a bitmap through an Intent.
  • Saving a whole list in onSaveInstanceState.
  • Thinking the limit is per Bundle, not shared.

Senior take & follow-ups

Give the shared-buffer detail and the 'pass IDs' rule.

Follow-up questions

Think of your answer first, then open the hint.

  1. When does the crash usually appear?

    Show hint

    When the Activity stops and its saved state is sent to the system.

  2. How do you pass a photo to another screen?

    Show hint

    Save it to a file and pass its URI or ID.

Suggest a fix if something here is wrong or out of date.