本文介绍了带屏幕旋转的 AsyncTask - onRetainNonConfigurationInstance 已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理 AsyncTask 和屏幕旋转时,我仍在尝试找到正确"的设计模式.我阅读了这篇公共软件博客文章,其中链接到这个代码,但onRetainNonConfigurationInstance 方法现已弃用.文档中的神奇句子指出在切换到下一个活动期间没有消息处理的保证简化了活动对象的使用......"这是关于此方法所做的真正关键的部分.我没有看到它使用 setRetainInstance() 的建议 能够实现相同的目标.

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.

我还看到了例如这个的帖子第一个答案实际上就是一个糟糕的黑客,它只涵盖了 90% 的用例(即,如果您的任务正在运行,然后有电话进来,该解决方案将不起作用,会发生什么).

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).

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

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.

推荐答案

它仅在 Honeycomb 及更高版本上被弃用,并且在那些上也能正常工作.新"方法是使用加载器(您可以使用兼容性库来获取 HC 之前版本中的加载器)或保留的片段.如果您调用 setRetainInstance() 实例传递给新创建的活动(他们实际上使用 onRetainNonConfigurationInstance 在兼容性库的 FragmentActivity 中),因此它实际上与您现在拥有的相同.

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.

这篇关于带屏幕旋转的 AsyncTask - onRetainNonConfigurationInstance 已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:27