I find myself using the Application class a lot to persist user data. These are application wide resources, though I cheat by storing an integer or two sometimes. Are there any drawbacks of doing this? I could not find any documentation which puts a limit on the amount of data that can be stored here.
1 Answer
Well, the documentation to Application says :
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way.
Also the stuff you put in there goes to the heap(*), which is size constrained (e.g. to 24 MB). If you want to store more data, you should put it in a database or on file system.
*) Technically Android's Dalvik vm may not have a heap, but other ways to store stuff in main memory.
1 Comment
Charlie Collins
Good answer, static singletons are ALSO a good approach. Still, I personally prefer the Application object because it has a well defined lifecycle. There really isn't anything particularly wrong with using it, no drawbacks, just keep the amount of data reasonable (this is for non persistent stuff you need to easily share between components -- for small data using Intents, for large persistent data use the filesystem or the database).