Reputation:
I need to store an ArrayList that will fetch data from Database, i am thinking of declaring that ArrayList in class that is subclass of android.app.Application class as i would be needing this frequently for search, sort etc purpose all over in my application.
1. It would be good approach to create get the Database instance in Application subclass.
2. Storing a list in the Application subclass.
I am not sure it is correct approach, i want to know what would be the drawbacks of this approach. Suggest if some other approach can be used.
Upvotes: 0
Views: 118
Reputation: 806
There are a few things you should take into consideration before deciding whether you're going with a Singleton design pattern versus the by Android provided Application class.
You may find some arguments that may help you choose in these SO posts:
And here is a great tutorial on databases in Android:
Upvotes: 0
Reputation: 44571
If I am understanding you correctly, you could set up a class to handle your DB actions and then create a reference to that class when needed, passing whatever parameters you need. You could have a function to return an ArrayList
if that's what you need. Depending on what you need, you may be able to implement this as an interface in the activities that need them.
Upvotes: 1
Reputation: 39856
I don't believe there're any drawbacks, despite the fact that you have to declare your Application on the manifest.
But you can easily achieve the same result creating a singleton which IMHO is a much cleaner way of doing it.
edit:
I just saw your edit and I'm editing my answer:
same thing goes to the DB object. Use a singleton! And on the first access you open the DB.
Upvotes: 1