Where it will be most correct to close the cursor? There is such a method that is called when creating a new record in the database, updating an existing record, deleting a record, and also in the onCreate method when launching an application:

private void fillList() { SQLiteDatabase database = dbHelper.getReadableDatabase(); Cursor cursor = database.query(DBHelper.TABLE_TASKS, null, null, null, null, null, null); String[] from = new String[]{DBHelper.COLUMN_CONTENT}; int[] to = new int[]{R.id.textTitle}; MySimpleCursorAdapter cursorAdapter = new MySimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0); listView.setAdapter(cursorAdapter); //cursor.close(); database.close(); } 

If you uncomment cursor.close (), the list will not have time to fill and an exception will be thrown out. In similar questions on English language, stackoverflow.com suggests closing the cursor in such methods as onStop or onDestroy, but the onDestroy method will also be called when you change the screen orientation along the onPasuse-> onStop-> onDestroy chain, and then the activation will be created anew. > onResume. How to be in this case? And, if, nevertheless, the proposed options are correct, then where is the best place to declare the cursor, because it will cease to be local.

I ask to help to understand where to initialize objects SQLiteDatabase and Cursor.

Or resources will not be used so madly that you can not bother and do not close the cursor? Although the studio strongly recommends doing this:

enter image description here

  • 2
    It is best to use the CursorLoader This class monitors the life cycle of the cursor on its own and correctly processes the device turns and other troubles and does not require additional care about when, where and what to close when working with SQLite, taking on these responsibilities. - pavlofff

0