使用startManagingCursor()的优点和缺点

问题描述:

现在我控制我的游标,如下所示。我想知道使用startManagingCursor()的好处是什么。就目前而言,我有很多游标,每一个知道,然后得到一个错误与他们做。如果不是更好的做法,这会有好处吗?使用startManagingCursor()的优点和缺点

Cursor c = db.rawQuery("GENERIC QUERY" , null); 
c.moveToFirst(); 
numval = c.getInt(c.getColumnIndex("_id"));     
c.close(); 

所有startManagingCursor首先是deperecated API http://developer.android.com/reference/android/app/Activity.html#startManagingCursor(android.database.Cursor

现在我们必须使用CursorLoader类LoaderManager。为了回答你的问题,如果活动正在管理光标,那么它可以在屏幕方向发生时进行优化。 Activitity会在其生命周期中关注游标的生命周期。以下是android doc的片段。

This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically

startManagingCursor捆绑光标生命周期的活动的生命周期。这包括在您的活动恢复时自动重新查询。我倾向于避免使用它,因为我不一定希望每次重新启动我的活动时重新执行查询。

就最佳实践而言,如果您希望在您的活动暂停时更新数据库,但即使如此,我仍然偏向于管理自己的光标,但它可能更有意义。我还希望让游标在短时间内保持打开状态,所以您的示例与我倾向于使用的模式相匹配。