带有屏幕旋转的 AsyncTask - onRetainNonConfigurationInstance 已弃用

发布于 2024-12-14 08:11:26 字数 1607 浏览 0 评论 0 原文

在处理 AsyncTask 和屏幕旋转时,我仍在尝试找到“正确”的设计模式。我阅读了 这篇 Commonsware 博客文章,其中链接到 <一个href="https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java" rel="nofollow noreferrer">此代码,但是 onRetainNonConfigurationInstance 方法现已弃用。文档中的神奇句子指出“在切换到下一个活动期间保证不进行消息处理简化了活动对象的使用......”这是该方法所做的真正关键的部分。我没有看到它使用 setRetainInstance() 的建议 能够实现相同的目标。

我还看到了帖子比如这个,其排名第一的答案这实际上是一个糟糕的黑客攻击,它只覆盖了 90% 的用例(即,如果您的任务正在运行,然后接到电话,该解决方案将不起作用,会发生什么情况)。

建议查看此处的代码对于 AsyncTask 示例,但除非我很密集,否则我看不到它们在应用程序中的任何地方使用(当使用 搜索功能在谷歌代码上)

这个问题显然已经被问过很多次了,但我还没有看到最新的、正确的答案。如果存在,请回答并关闭重复(如果必须),但至少回答! :)

这是另一个链接< /a> 审查相同的、已弃用的方法。

I am still trying to find the "correct" design pattern when dealing with AsyncTasks and screen rotation. I read this commonsware blog post which links to this code, but the onRetainNonConfigurationInstance method is now deprecated. The magic sentence in the documentation states "The guarantee of no message handling during the switch to the next activity simplifies use with active objects..." which is a truly critical part about what this method did. I don't see its suggestion of using setRetainInstance() being able to accomplish the same goal.

I also saw posts such as this one whose number one answer is really nothing short of a bad hack which only covers 90% of use cases (ie. what happens if your task is running and then a phone call comes in, that solution will not work).

It was suggested to review the code here for AsyncTask examples but unless I am being dense, I don't see them being used anywhere in the application (when using the search feature on google code)

This question has obviously been asked many times but I have not seen an up to date, proper asnwer. If one exists, please answer and close as duplicate if you must, but at least answer! :)

This is another link which reviews the same, deprecated method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2024-12-21 08:11:26

它仅在 Honeycomb 及更高版本上被弃用,并且它在这些上也可以正常工作。 “新”方法是使用加载器(您可以使用兼容性库来获取 HC 之前版本中的加载器)或保留片段。如果您调用 setRetainInstance() 实例将按原样传递给新创建的 Activity(它们实际上在兼容性库的 FragmentActivity 中使用 onRetainNonConfigurationInstance) ,所以它实际上与您现在拥有的相同。

It's only deprecated on Honeycomb and above, and it will work just fine on those too. The 'new' way is to use loaders (you can use the compatibility library to get those in pre-HC versions) or retained fragments. If you call setRetainInstance() the instance passed as is to the newly created activity (they actually use onRetainNonConfigurationInstance in the FragmentActivity of the compatibility library), so it's effectively the same as what you have now.

双手揣兜 2024-12-21 08:11:26

使用已弃用的方法本身并没有什么问题。如果您的 AsyncTask 非常重要,以至于您无法在方向改变时取消它并重新启动它,那么您应该考虑使用服务。

There's nothing inherently wrong with using a deprecated method. If your AsyncTask is THAT critical that you can't cancel it and start it up again if your orientation changes, you should consider using a service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文