2

I am using three activities which are opened at the same time. All activities are retreive data from sqlite. I don't close or re open my connection when i am going from activity a->b or from b->c. I just dispose my db when activity is destroying.

Activity A

SqliteConnection db; OnCreate method db = new SqliteConnection(mypath); OnDestroy db.Dispose(); db=null; Onbuttonclick startActivity(new Intent(this, ActivityB)); 

Same code is running when i am going from activity b->c. Inside the same activity i use sqlite plenty of times.

Is this a good practice? Should i dispose my connection immediatelly after a use? Or should i close my connection on pause and reopen on resume? Or can i pass the same opened connection to the next activity? Which is the best approach?

Question modifieded

class databaseHelper { private static SqliteConnection db; public static SqliteConnection openDatabase(Context context) { if(db==null) db = new SqliteConnection(mypath); return db; } } 

And inside my activity on create

 databaseHelper.openDatabase(this).myquery.... 
3
  • Create a static instance of your database and use it anywhere you want. This way, it only gets initialized one time and can be used throughout the application. The instance gets destroyed when your app process is destroyed. Commented Apr 28, 2019 at 8:02
  • Can you give me an example? Creating a static void and returning an sqlite conncection? Commented Apr 28, 2019 at 8:05
  • Added an example, hope you can understand it. Commented Apr 28, 2019 at 8:19

2 Answers 2

5

I don`t roll with Java nor xamarin. Here is a Kotlin code, it is pretty self-explanatory.

class DatabaseHelper { //Public class companion object { ///This is equiavalent to java static. private var instance: YourDatabase? = null fun getDbInstance(context: Context): YourDatabase? //This functions returns the initialized DB instance. { if(instance == null) instance = YourDatabase(context) // initializing the DB only one time return instance } } } 

Just create a public class and name it for example "DatabaseHelper". Inside the class, create one static variable of your database type. Create a public function that returns the static variable. Inside the function, first, check if the static instance is null and if it is null, then initialize it with your database instance. This way, when you need to use your database instance, just, access the static function, provide it with the context and it will return you the initialized database instance.

In Kotlin

DatabaseHelper.getDbInstance(this).yourDbFunction() 

UPDATE

Since this answer took off, I would like to suggest improvements to my previous solution. Instead of passing a context of activity to initialize the database, use application context. If you give an activity context to the static database instance, a memory leak will occur because the database instance holds a strong reference to the activity and the activity will NOT be eligible for garbage collection.

Proper usage:

val myDb = MyDb(applicationContext) 
Sign up to request clarification or add additional context in comments.

15 Comments

Should i ever need to close or dispose my sqliteConnection? Or it is safe to keep it always open?
No need to worry about disposing of the connection yourself. It will self dispose of when your app process is killed. There is nothing wrong with keeping the connection open. Opening connection to DB is kind of expensive, so using the already open connection whenever you want to perform the query is efficient than closing and reopening the connection again.
Thank you! So i dont need to care of dispose my connection each time an activity is destroyed right? Also shoud i create, update insert and delete tasks inside this class or it is not recommended?
Yes, as I mentioned before, you do not need to worry about closing it. Do not put your database queries inside this DatabaseHelper class. Its only job is to provide it with the initialized DB instance. This class allows you to have access to your database. Your queries should be handled inside your SQLite class.
Please check my modifiede question and tell me if i am somewhere wrong
|
0

In general we should encapsulate access to a local store in another class such as a DAO/Repository/Service instead of having them directly in the Activity. this promotes loose coupling between views and data/network access. This also decouples the lifecycle of your DB connection, from the lifecycle of the currently running activity, giving you more control and opportunity for reuse.

Try using a bound Service to and have your DB connections there. Because it is a bound Service, it'll only be around if there is an Activity around that binds to it. Each Activity will bind to the same instance of the Service so it means you wont have duplicate connections. When no Activities are bind to it, it'll automatically be destroyed, destroying the connection along with it.

For a more modern, structured approach, using Jetpack components, you can look at https://github.com/googlesamples/android-sunflower

2 Comments

Thank you for the info they are very valuable! Using this above will also prevent from exception that my database is locked?
There's no need to use a Service, you've got the separation of concerns wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.