patternjavaMinor
Android global data
Viewed 0 times
globaldataandroid
Problem
I have a huge list of song objects in my program and I need those objects in almost all activities. Well, at least a part of it up to everything.
So I created a class which looks pretty much like this :
So this
Now I am wondering, is that a good approach? Should I do anything different to create global variables I can use in every activity?
Also, I saw a few questions about Singletons. Is this a Singleton class?
So I created a class which looks pretty much like this :
class DataStore {
private static ArrayList songList;
private static ArrayList albumList;
private DataStore()
{
}
//getters and setters for the private variables above
public void getSongList() { ... }
...
}So this
DataStore has a lot of private variables and getters / setters to access them. I call these functions like this :ArrayList songList = DataStore.getSongList();Now I am wondering, is that a good approach? Should I do anything different to create global variables I can use in every activity?
Also, I saw a few questions about Singletons. Is this a Singleton class?
Solution
I have a huge list of song objects in my program and I need those objects in almost all activities.
Your description is an excellent fit for Content Providers.
http://developer.android.com/guide/topics/providers/content-providers.html
Implementing a content provider for your songs might seem like a lot of work at first, but it will be worth the investment. It's the clean and recommended approach, and you'll benefit from it greatly. As an added bonus, other applications will be able to use your song "database" too. Go for it.
About the singleton pattern, read this:
http://en.wikipedia.org/wiki/Singleton_pattern
And avoid using singletons as much as possible.
Your description is an excellent fit for Content Providers.
http://developer.android.com/guide/topics/providers/content-providers.html
Implementing a content provider for your songs might seem like a lot of work at first, but it will be worth the investment. It's the clean and recommended approach, and you'll benefit from it greatly. As an added bonus, other applications will be able to use your song "database" too. Go for it.
About the singleton pattern, read this:
http://en.wikipedia.org/wiki/Singleton_pattern
And avoid using singletons as much as possible.
Context
StackExchange Code Review Q#56175, answer score: 5
Revisions (0)
No revisions yet.