当有运行AsyncTasks手柄屏幕方向变化 [英] Handle screen orientation changes when there are AsyncTasks running
问题描述
我一直在窃听通过这一段时间。我该如何妥善处理屏幕方向变化,而我有一个单独的发
/ 的AsyncTask
运行?目前,我有
I've been bugged by this for a while. How do I properly handle screen orientation changes while I have a separate Thread
/ AsyncTask
running? Currently, I have
android:configChanges="orientation|keyboard|keyboardHidden"
在我的的AndroidManifest.xml
,但那是的没有真正鼓励:
注意:使用这个属性应避免使用仅作为最后手段。请由于配置改变读取处理运行时更改了有关如何正确处理重新启动的详细信息。
Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
此外,在 2.3模拟器,它切换时风景
,但切换回人像工程code>失败。
Also, in the 2.3 emulator, it works when switching to landscape
, but switching back to portrait
fails.
现在,我之所以用 configChanges
是因为当用户切换方向,我可能有一个的AsyncTask
跑步,做一些网络流量,而我不希望它停止。
Now, the reason why I use configChanges
is because when the user switches orientation, I might have an AsyncTask
running, doing some network traffic, and I don't want it stopped.
是否有这样做的任何其他方式,或者是有固定2.3切换回肖像的方式?
Is there any other way of doing this, or is there a way of fixing 2.3 to switch back to portrait?
我知道 onRetainNonConfigurationInstance
,但我不知道这将是一个好主意,拯救的AsyncTask
的实例,主要是因为扩展了的AsyncTask
类不是静态的(所以它是联系在一起的活动
) - - 和它需要的,因为在 onPostExecute()
它调用从活动方式
实例
I know about onRetainNonConfigurationInstance
, but I'm not sure it would be a good idea to "save" the AsyncTask
instance, mainly because the class that extends AsyncTask
is not static (so it is tied to the Activity
) -- and it needs to be, because in onPostExecute()
it calls methods from the Activity
instance.
推荐答案
我有一个类似的问题会向您和工作围绕它通过执行的AsyncTask
作为类的一部分从<一个继承href="http://developer.android.com/reference/android/app/Application.html"><$c$c>Application$c$c>类。一个应用程序
类是可用的应用程序的所有生活时间,让你不必担心你的的AsyncTask
获得中断,除非整个应用程序就会被杀死。
I had a similar problem to your and worked around it by implementing the AsyncTask
as part of a class which inherits from Application
class. An Application
class is available all the life time of the application So you don't have to worry about your AsyncTask
getting interrupted unless the whole application will be killed.
要得到通知时,任务已经完成了活动
必须实现它来给自己注册到应用程序$ C A接口$ C>类。
To get notified when the task has finished the Activity
has to implement a interface which it uses to register itself to the Application
class.
由于屏幕旋转,你可以注销你的活动的当应用程序被破坏
从应用程序
类和重-register它时,它被重建。如果任务的破坏和娱乐之间完成操作的结果可以存储在应用程序
类同时让活动
可以检查任务是否仍在运行或结果是否已经可用时,它被重建。
When your application is destroyed because of the screen rotation you can unregister your Activity
from the Application
class and re-register it when it is recreated. If the task finishes between destruction and recreation the result of the operation can be stored in the Application
class meanwhile so the Activity
can check whether the task is still running or whether the result is already available when it is recreated.
另外一个好处是,你可以直接访问应用程序的情况下,因为应用程序
类是一个子类的上下文
类。
Another advantage is that you have direct access to the applications context because the Application
class is a sub class of the Context
class.
这篇关于当有运行AsyncTasks手柄屏幕方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!