Android ViewModel came to RESCUE (TransactionTooLargeException)

Mohit Charadva
MindOrks
Published in
3 min readAug 3, 2017

The reason why I wrote this article is to share my experience of my encounter with TransactionTooLargeException :P

Recently I was testing my application in Nexus 5x with Android Nougat OS and my application was crashing each time I try to move from one activity to another activity. It was strange for me because the application was working on other device without a single crash. Then I checked logcat and found below stackstrace.

Stacktrace

Looking at the stacktrace, I was not able to figure out which activity is causing the crash.

TooLargeTool

TooLargeTool is a nice little library which can help you debug TransactionTooLargeExceptions on Android 7.

onSavedInstanceState

With the help of this TooLargeTool, I could find out an activity which was causing the crash. In this activity, I was storing data into bundle so that I can retrieve it later in onRestoreInstanceState to restore the previous state of the activity without calling web-service to fetch the data. It was my parcelable object which was storing almost 600kb data in it and eventually causing my application to crash.

Exact maximum size is not specified in the android documentation.

As we found the issue, now question is how we will solve this crash.

ViewModel

ViewModel class is part of Android Architecture Components, which were release in Google IO 2017.

As per the android documentation “The ViewModel class is designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations”.

To understand ViewModel scope with respect to Activity lifecycle, have a look at a below diagram.

Generally, instance of ViewModel class created in onCreated() when it is requested for the first time and it survives until the requesting activity gets finished and destroyed. onCreate() can be called many times based on the configuration changes but ViewModel designed to survive though those scenarios and same ViewModel instance can be retrieved to reload UI with data.

Solutions to prevent TransactionTooLargeException

  1. Add dependencies required for ViewModel class
  2. Create a separate class which extends ViewModel class.
  3. Move all your data variable to newly created class.
  4. Setup bridge between ViewModel and UI-Controller (In our case it will be Activity) for the communication.

Step 1: Add dependecies

Modify, project level build.gradle like below:

Add below lines to module level build.gradle under dependencies.

Step 2: Create ViewModel class

Step 3: Associate the Activity and ViewModel

ViewModelProviders.of(<Your UI controller>).get(<Your ViewModel>.class)

Make sure your ViewModel should not hold the reference of Activity, Fragment, Context etc.

Step 4: User ViewModel in your Activity

Conclusion

We have eliminated onSavedInstanceState which was not able to handle large amount of data and replaced it with ViewModel which survives configuration changes and can handle large amount of data.

References

If you liked this post, please hit the little heart! ❤

--

--