startManagingCursor 的目的是什么?
好的,文档指出它让 Activity 管理光标的生命周期。但我并没有真正看到它的意义,因为当活动被销毁时,对新创建的游标的任何引用也应该被删除,然后游标本身将在下一个垃圾收集周期中消失。那为什么还要麻烦呢?
Ok, the documentation states that it lets the Activity manage the cursor's lifecycle. But I don't really see the point of it since when the activity is destroyed, any references to the newly created cursor should be also erased and then the cursor itself is left to perish in the next garbage collecting cycle. So why bother?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该依赖垃圾收集器销毁游标...游标代表大量资源:游标保存的所有数据,加上与拥有游标的内容提供程序的连接,这也意味着要求其过程被保存在内存中。
在最新版本的 Android 中,如果游标的终结器在未显式关闭的情况下运行,则会打印日志消息,因为应用程序在使用完游标后关闭游标非常重要。
托管游标负责在活动被销毁时关闭游标,但它们的作用还不止于此:当活动停止和重新启动时,它们将被停用并重新查询。
也就是说,此时您应该考虑弃用托管游标。新的 Loader API 好多了,并且对应用程序的用户体验进行了许多改进 - 它确保所有光标操作都在主线程之外完成(因此 UI 交互和动画中不会出现故障),并且可以当由于配置更改而重新启动活动时,在活动实例之间传播现有游标数据,而不必重新加载数据。
如果您需要在低于 3.0 的 Android 版本上运行,您可以为这些应用程序使用 v4 支持库的 Loader 实现。
You should not rely on cursors being destroyed by the garbage collector... a cursor represents a significant amount of resources: all of the data held by the cursor, plus the connection to the content provider that owns the cursor which also means requiring that its process be kept in memory.
In more recent versions of Android, log messages are printed if a cursor's finalizer runs without being explicitly closed, because it is important for apps to close cursors when done with them.
Managed cursors take care of closing the cursor when the activity is destroyed, but they do more than that as well: they will be deactivated and requeried as the activities is stopped and restarted.
That said, at this point you should consider managed cursors to be deprecated. The new Loader API is a lot better, and has many improvements to the user experience of your app -- it ensures that all cursor operations are done off the main thread (so there are not glitches in your UI interactions and animations), and can propagate existing cursor data across activity instances when an activity is restarted due to a configuration change instead of having to reload the data.
If you need to run on older versions of Android than 3.0, you can use the v4 support library's implementation of Loader for those applications.