2

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?

4

1 回答 1

1

您怀疑您对 Application 的使用是对此类的轻微滥用是正确的。特别是,您正在将其转变为数据库。在尝试跨活动保持数据的一致状态时,您已经看到了这个问题。

您可以利用 Android 对SQLite的内置支持,而不是这样做。如果你想更进一步,直接获取对象,你可以使用OrmLite for Android

于 2013-06-27T10:53:56.300 回答