Why is DBHelper not closed in the Android notepad tutorial?

As per startManagingCursor documentation, it only handles cursors. So I believe it doesn't do anything with database connection as it may need to re-query data in future.

Up vote 0 down vote favorite share g+ share fb share tw.

I am currently writing my first Android app and have based most of my knowledge on the Android notepad tutorial: developer.android.com/resources/tutorial... In my application I am using multiple DBHelpers in one Activity and not every Cursor is managed by the activity with startManagingCursor(). I have learned that every DB connection has to be opened and closed propperly: SQLiteOpenHelper.open(); Cursor.open(); //use cursor Cursor.close(); SQLiteOpenHelper.close(); As far as I know startManagingCursor() does this work for you. But does startManagingCursor() also open and close the SQLiteOpenHelper?

The Android Notepad tutorial is using startManagingCursor() but the DBHelper is never closed. Why is the SQLiteOpenHelper never closed? EDIT: This is my current code.

It is using one SQLiteOpenHelper called mDriverDbHelper. This code is an adoption from the tutorial: private DriverDbAdapter mDriverDbHelper; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.

OnCreate(savedInstanceState); setContentView(R.layout. Bus_selectuser); mDbHelper = new DbAdapter(this); mDbHelper.open(); mDbHelper.close(); mDriverDbHelper = new DriverDbAdapter(this); Log. W("BuerBusActivity", "opening DB connection via DbHelber now"); mDriverDbHelper.open(); fillData(); //request the screen to stay on this.getWindow().

AddFlags(WindowManager.LayoutParams. FLAG_KEEP_SCREEN_ON); } @Override public void onRestart() { super.onRestart(); Log. V(TAG, "onRestart"); } @Override public void onStart() { super.onStart(); mDriverDbHelper.open(); Log.

V(TAG, "onStart"); } @Override public void onResume() { super.onResume(); Log. V(TAG, "onResume"); } @Override public void onPause() { Log. V(TAG, "onPause"); super.onPause(); } @Override public void onStop() { mDriverDbHelper.close(); Log.

V(TAG, "onStop"); super.onStop(); } This code sample does work but I don't understand why. I am calling mDriverDBHelper.open() twice in onCreate and in onStart. I have tried to put the open() and close() calls in onPause and onResume but this results in an error: Cursor: invalid statement in fillWindow() Does anybody know why it has to be onStart and onStop and not onPause and onResume?

FINAL ANSWER The tutorial is missing a close() method call. A general rule to using open and close in conjunction with the activity life-cycle is: After opening an SQLiteOpenHelper in an activity life-cycle-method you should close it in the corresponding counterpart life-cycle-method. @Override public void onCreate() { .... //open SQLiteOpenHelper onCreateHelper.open(); } @Override public void onStart() { .... //open SQLiteOpenHelper onStartHelper.open(); } @Override public void onResume() { .... //open SQLiteOpenHelper onResumeHelper.open(); } @Override public void onPause() { .... //close SQLiteOpenHelper onResumeHelper.close(); } @Override public void onStop() { .... //close SQLiteOpenHelper onStartHelper.close(); } @Override public void onDestroy() { .... //close SQLiteOpenHelper onCreateHelper.close(); } In the case of the Android NotePad Tutorial the onDestroy() method was missing and should close the mDbHelper.

Android sqliteopenhelper link|improve this question edited Apr 5 at 13:36 asked Apr 3 at 15:14bas33.

As per startManagingCursor documentation, it only handles cursors. So I believe it doesn't do anything with database connection as it may need to re-query data in future. From Android Developer's site: This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle.

That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically. Regarding database connection closing in android notepad tutorial, I think they have missed to close it in their sample code.

So no big deal :).

– bas Apr 3 at 15:34 yes, open db connection in onStart or onResume and close it in onStop or onPause, respectively – Waqas Apr 3 at 15:44 It seems only onStart() and onStop() work properly – bas Apr 3 at 16:22 its fair enough :) – Waqas Apr 3 at 17:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions