android中Activity.startManagingCursor(cursor)方法详解
2015-08-09 14:00
519 查看
在使用数据库操作查询数据后,如果是在Activity里面处理,那么很可能就会用到startManagingCursor()方法,
startManagingCursor()作用和使用注意事项:
调用这个方法,就是将获得的Cursor对象交与Activity 来管理,这样Cursor对象的生命周期便能与当前的Activity自动同步,省去了自己管理Cursor。
看API中的注释
This method allows the activity to take care of managing the given
lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call
the given Cursor, and when it is later restarted it will call
you. When the activity is destroyed, all managed Cursors will be closed automatically. If you are targeting
later, consider instead using
available via
Warning: Do not call
cursor obtained from
will do that for you at the appropriate time. However, if you call
a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call
记住以下三条:
1.这个方法使用的前提是:游标结果集里有数据记录。
所以,在使用之前,先对Cursor是否为null进行判断,如果Cursor != null,再使用此方法
2.如果使用这个方法,最后也要用stopManagingCursor()来把它停止掉,以免出现错误。
3.使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,省去自己手动管理。如果不调用,那么很多工作就要你自己来做,在Activity被stopped时,调用Cursor.deactivate,在restart的时候调用Cursor.requery,还有其他状态也最好都与Activity保持同步,以免出现一些错误,不过,为什么不用现成的startManagingCursor,省时又省力.
startManagingCursor()作用和使用注意事项:
调用这个方法,就是将获得的Cursor对象交与Activity 来管理,这样Cursor对象的生命周期便能与当前的Activity自动同步,省去了自己管理Cursor。
看API中的注释
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
Cursor.deactivateon
the given Cursor, and when it is later restarted it will call
Cursor.requeryfor
you. When the activity is destroyed, all managed Cursors will be closed automatically. If you are targeting
android.os.Build.VERSION_CODES.HONEYCOMBor
later, consider instead using
LoaderManagerinstead,
available via
getLoaderManager().
Warning: Do not call
Cursor.close()on
cursor obtained from
managedQuery, because the activity
will do that for you at the appropriate time. However, if you call
stopManagingCursoron
a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call
Cursor.close().
记住以下三条:
1.这个方法使用的前提是:游标结果集里有数据记录。
所以,在使用之前,先对Cursor是否为null进行判断,如果Cursor != null,再使用此方法
2.如果使用这个方法,最后也要用stopManagingCursor()来把它停止掉,以免出现错误。
3.使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,省去自己手动管理。如果不调用,那么很多工作就要你自己来做,在Activity被stopped时,调用Cursor.deactivate,在restart的时候调用Cursor.requery,还有其他状态也最好都与Activity保持同步,以免出现一些错误,不过,为什么不用现成的startManagingCursor,省时又省力.
相关文章推荐
- Android Activity 知识点全面总结
- Android工程各个文件夹的功能区别
- android内存优化
- 使用Android Studio 1.3 版本进行NDK开发准备工作
- android 自定义控件中获取属性的三种方式(转)
- Android应用程序(APK)的构建过程
- Android生命周期函数执行顺序
- android 自定义组件-带图片的textView
- Android手势密码LockPatternView、LockPasswordUtils、LockPatternUtils等分析
- NavigationView+Toolbar
- Android之SurfaceView
- android4.02在x4412平台编译碰到的问题
- AndroidStudio更换黑色主题方法
- Android Handler的使用(3)
- android中画文字的换行 办法(对于遇到canvas.drawText(String s )无法实现换行问题的解决)
- android学习笔记(8)linearlayout与android:layout_weight学习
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android 的消息机制
- 使用SpannableString设置部分文字大小、颜色、超链接、点击事件
- Android核心基础-8.Android四大组件之Activity-1.创建、启动