startManagingCursor() for cursor

在android中访问数据库的要用到cursor对象:

cursor = db.query(DbHelper.TABLE, null, null, null, null, null,
   DbHelper.C_CREATED_AT + " DESC"); //

startManagingCursor(cursor); //

// Iterate over all the data and print it out
String user, text, output;
while (cursor.moveToNext()) { //
    user = cursor.getString(cursor.getColumnIndex(DbHelper.C_USER)); //
    text = cursor.getString(cursor.getColumnIndex(DbHelper.C_TEXT));
    output = String.format("%s: %s\n", user, text); //
    textTimeline.append(output); //
}
通过数据库拿数据创建了一个Cursor对象之后,使用方法startManagingCursor处理一下cursor对象,能使得当前activity即将死傲视的时候,释放这个cursor对象所指的数据,方便java的垃圾回收。下面是从《Learning Android》上摘录的英语原文:

startManagingCursor() is a convenience method that tells the activity to start managing the cursor’s life cycle the same way it manages its own. This means that when this activity is about to be destroyed, it will make sure to release any data referred to by the cursor, thus helping Java’s garbage collector clean up memory more quickly. The alternative is for us to add code manually in various override methods and worry about cursor management ourselves.

你可能感兴趣的:(startManagingCursor() for cursor)