I have an application that reads values โโfrom a database table (the database is called SoftCopyDatabase) and populates the list with values โโread from the database. When you click on an item from the list, a new activity begins.
The problem is that when I press the back key, I got an error
IllegalStateException: database already closed
My code is as follows:
The OpenClick public class extends ListActivity {
public static String subjectName; private SoftCopyDatabase lectures; private static int[] subTO = { R.id.subject }; private static String[] subFROM = { SUBJECT }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lectures = new SoftCopyDatabase(this); } public void onStart() { super.onStart(); try { Cursor cursor = getSubjects(); showSubjects(cursor); } catch (Exception e) { e.printStackTrace(); } } public void onRestart() { super.onRestart(); lectures = new SoftCopyDatabase(this); } public void onStop() { super.onStop(); lectures.close(); } public void onDestroy() { super.onDestroy(); lectures.close(); }
}
One point that I would like to mention is to remove the onStop () method, which the application is working correctly. But I have to enable onstop () because I want to control the opening and closing of the database.
source share