Would it be OK to have a single instance of SQLiteOpenHelper as a member of a subclassed Application, and have all Activities that need an instance of SQLiteDatabase get it from the one helper?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Having a single
SQLiteOpenHelperinstance can help in threading cases. Since all threads would share the commonSQLiteDatabase, synchronization of operations is provided.However, I wouldn’t make a subclass of
Application. Just have a static data member that is yourSQLiteOpenHelper. Both approaches give you something accessible from anywhere. However, there is only one subclass ofApplication, making it more difficult for you to use other subclasses ofApplication(e.g., GreenDroid requires one IIRC). Using a static data member avoids that. However, do use theApplicationContextwhen instantiating this staticSQLiteOpenHelper(constructor parameter), so you do not leak some otherContext.And, in cases where you aren’t dealing with multiple threads, you can avoid any possible memory leak issues by just using one
SQLiteOpenHelperinstance per component. However, in practice, you should be dealing with multiple threads (e.g., aLoader), so this recommendation is only relevant for trivial applications, such as those found in some books… 🙂