Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 925
leojg Avatar asked Nov 09 '12 12:11

leojg


1 Answers

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

like image 67
Zsombor Erdődy-Nagy Avatar answered Sep 19 '22 11:09

Zsombor Erdődy-Nagy