Passing large data to an activity

Posted on Feb 3, 2013

sending large data to an activity

photo by The Consumerist (http://www.flickr.com/photos/consumerist/)

The issue with arguments…

Recently I have encountered a problem while passing a large data to an activity. There was a need to pass around 10 megabytes of data in an object array from one activity to another, and while trying to do that the second activity would not start at all and in my case (app’s min version 4.0) Android would not give me any clue (exception) of an issue. As soon as I realized what the problem was I started looking into solutions.

There are several ways to post data to an activity, and the recommended (official, by Google) one is using Intents with extras. Here is a usual way to achieve this:

For many occasions using Intent extras is the best way to submit data from one activity to another. The problem comes when you try to pass large data to an activity (more than 1 MB). There was no such need in early Android versions since the actual devices had very limited RAM (heap memory). Newer devices are bundled with much larger RAM and since sending objects directly (via a reference, as opposed to persisting data in first activity and retrieving it in a second one) is the fastest way to pass large data to an activity I decided to do a research and find a way around the issue.

So, how do we pass large data to an activity?

There are several suggestions to achieve this, through defining global variables, static fields, member variables, etc. Having tweaked those ones a bit to get clean encapsulated code, I decided to implement it using Java enum. Here is the code:

  • Edick

    thank you)))