VM4
VM4

Reputation: 6501

Overusing the Application class to store persistent data

I'm making an application that has to share a lot (relatively) of data between activities. At first I was passing the data between activities by serialising it or making it parcelable, but it got messy pretty fast (especially when I need to preserve data when a user presses BACK). Now I decided to use the Application class to store it in a global singleton. Now it got a lot cleaner and much more simple (and this is the main reason why I want to do it). Not that it's relevant but it's a restaurant ordering application and the data I'm storing as global is:

  1. A list of all menu items in categories. HashMap < Integer, ArrayList> (I get them from a server so it might change between launches).
  2. The cart with all the stuff a user added to it HashMap < Integer, HashMap >
  3. A simple string.

The question is simple: How would the "big boys" do this? Am I correct to use the Application class? Do I need to worry about storing too much stuff here? Should I just drop this and go with the Serializable/Parcelable stuff and constantly put them in Intent's?

Upvotes: 2

Views: 124

Answers (1)

mikołak
mikołak

Reputation: 9705

You're correct in suspecting that your use of Application is a slight abuse of this class. In particular, you're on your way to turning it into a database. You're already seeing problems with this in the issue of attempting to keep a consistent state of your data across Activities.

Instead of doing that, you can utilize Android's built-in support for SQLite. If you want to take a further shortcut and obtain objects directly, you can use OrmLite for Android.

Upvotes: 1

Related Questions