1

I have a code like this:

void showHistory(final long alertid) {
    final View view = getLayoutInflater().inflate(R.layout.dialog_history, null);
    ListView listView = (ListView) view.findViewById(R.id.list_history);
    final Cursor cursorHistory = helper.getHistory(alertid);
    CursorAdapter cursorAdapter = new HistoryAdapter(this,cursorHistory);
    listView.setAdapter(cursorAdapter);

    new AlertDialog.Builder(this)
            .setTitle("History")
            .setView(view)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .show();
}

My question is - where the cursorHistory shall be closed? Do I need to write a special handling for that, or is it closed automatically somewhere on cursor's finalization?

Mixaz
  • 4,068
  • 1
  • 29
  • 55

4 Answers4

2

You should close the cursor in onPause() and reopen it in onResume().

But to avoid the Cursor problems, you should NOT pass the Cursor object to the Adapter, get your data from the Cursor and close it, then pass the data to the Adapter.

Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • Looks like a candidate for the answer. I saw people prefetch data from cursor into array, but it looks awkward (but probably quite suitable for my case since I do not have many records in the cursor). Could you please provide any reference to docs / posts at SO about closing cursors in onPause()? – Mixaz Nov 12 '14 at 01:23
0

I think you do not have to close the cursor. It will be handled by the adapter.

vandus
  • 3,248
  • 3
  • 30
  • 44
  • I guess the same, but could you provide any reference to specs to prove? – Mixaz Nov 12 '14 at 01:16
  • 1
    seems that it's a wrong answer, as the cursor will be closed not by adapter but by GC on cursor's object finalization. It is not recommended due to waste of resources associated with the cursor. The right answer is to close and re-create the cursor on **onPause/onResume**, see others answers – Mixaz Nov 12 '14 at 02:01
0

Here go some info I have found on the subject: there's a deprecated method Activity.startManagingCursor() which was intended to close/requery the cursor in onPause()/onResume(), as suggested in the answer by @Abdullah

It was deprecated in favor of LoaderManager class which allows downloading cursor's data in background thread, while startManagingCursor works on UI thread.

Read this tutorial for more info: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html Borrowed from here: https://stackoverflow.com/a/19652004/1028256

Community
  • 1
  • 1
Mixaz
  • 4,068
  • 1
  • 29
  • 55
0

Call changeCursor(null); in the onPause().

From the documentation :

void changeCursor (Cursor cursor)
Change the underlying cursor to a new cursor. If there is an existing cursor it will be closed.

Link to the changeCursor documentation

Pmax
  • 175
  • 1
  • 6