在 AsyncTasks 运行时处理屏幕方向变化 [英] Handle screen orientation changes when there are AsyncTasks running

查看:15
本文介绍了在 AsyncTasks 运行时处理屏幕方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个问题困扰了一段时间.当我运行单独的 Thread/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 中,但那是 不鼓励:

in my AndroidManifest.xml, but that is not really encouraged:

注意: 应避免使用此属性,并且仅将其用作最后的手段.请阅读处理运行时更改以获取有关如何正确处理由于配置更改而导致的重启的更多信息.

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模拟器中,切换到landscape时也能工作,但切换回portrait时失败.

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 不是静态的(因此它与 Activity 相关联)——它需要是,因为在 onPostExecute() 中它从Activity 实例.

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 作为继承自 Application 类.Application 类在应用程序的整个生命周期都可用,因此您不必担心 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.

要在任务完成时得到通知,Activity 必须实现一个接口,用于将自身注册到 Application 类.

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.

当您的应用程序因屏幕旋转而被破坏时,您可以从 Application 类中注销您的 Activity,并在重新创建时重新注册它.如果任务在销毁和重新创建之间完成,操作的结果可以同时存储在 Application 类中,以便 Activity 可以检查任务是否仍在运行或结果重新创建时已经可用.

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.

另一个优点是您可以直接访问应用程序上下文,因为 Application 类是 Context 类的子类.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆