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.

转载于:https://my.oschina.net/gesuper/blog/167683

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值