Search code examples
androidsqlitesqliteopenhelperandroid-context

How does it work SQLiteOpenHelper Context


I have an app with two activities.

One of them list several values while the other add new values to a DB.

I have a class wich extends from SQLiteOpenHelper and manages the DB connections, queries, etc.

Now, I understand that in the constructor of SQLiteOpenHelper you have to pass a context which is used to determine if it has to create a new DB or open an existing one.

But if I have one instance of the SQLiteOpenHelper class in each activity, then the context would be different.

Is there a way to avoid this?

Thanks.


Solution

  • Pass in the Application context, (.getApplication()) instead of the Activity. That way, both instances will access the db using the same context.