If cursor.moveToFirst() returns false do I still need to close it?
Since it returns false when the cursor is empty.
I'm doubt about the correct approach:
if (cursor != null && cursor.moveToFirst()) {
    // some code
    cursor.close();
}
Or:
if(cursor != null) {
    if (cursor.moveToFirst()) {
        // some code
    }
    cursor.close();
}
				
                        
You must close all
Cursorswhich are not nulls, regardless of whether they have populated entries or not.The only exception to the above statement would be if you know that the
Cursorin question is managed by some "external" framework and will be automatically closed without your interaction (as is the case withLoaderManagerframework when used withCursorLoader).At least two (good) reasons to close any non-null
Cursor:Cursorscan have "memory-allocations" which need to be explicitly released even if they are empty (as is the case withAbstractWindowedCursor)Cursorcan become non-empty ifrequery()is called. Your means to prevent this explicitly is to close theCursorThe most general and error prone approach would be (it is an overkill in some cases):
Another popular pattern if you need to iterate over
Cursor'sentries:Don't feel bad with one extra
c != nullcomparison - it is totally justified in these cases.