Android 2.3.3
I have two activities in my application that access the database. Whenever I change the orientation of the mobile, i get the following errors in logcat, though the program does work. I understand from the errors that I have to call close() and close the database and cursors. But where do i put it exactly.
Will closing the database and cursors in onDestroy() of the activity, solves the issue?
Database pm;
Cursor c;
@Override
public void onDestroy() {
pm.close();
c.close();
// Any other cursors that are used...
super.onDestroy();
}
Here is the logcat trace incase needed…
02-16 22:37:50.419: E/Database(602): close() was never explicitly called on database 'portfolio'
02-16 22:37:50.419: E/Database(602): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:822)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:856)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:849)
02-16 22:37:50.419: E/Database(602): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:553)
02-16 22:37:50.419: E/Database(602): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
02-16 22:37:50.419: E/Database(602): at com.xx.xxx.Database.<init>(Database.java:21)
02-16 22:37:50.419: E/Database(602): at com.xx.xxx.Brokerage_Settings.onCreate(Brokerage_Settings.java:118)
02-16 22:37:50.419: E/Database(602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-16 22:37:50.419: E/Database(602): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 22:37:50.419: E/Database(602): at android.os.Looper.loop(Looper.java:130)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-16 22:37:50.419: E/Database(602): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 22:37:50.419: E/Database(602): at java.lang.reflect.Method.invoke(Method.java:507)
02-16 22:37:50.419: E/Database(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-16 22:37:50.419: E/Database(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-16 22:37:50.419: E/Database(602): at dalvik.system.NativeStart.main(Native Method)
What is the best practise to close the database and cursor objects?
Call close in onPause(). When you change orientation of mobile your app changes state and you are supposed to close database/cursor object before the super.onPause() is called by android system. Call close in onPause method before the command super.onPause().