当 AsyncTasks 运行时处理屏幕方向变化
我已经被这个困扰有一段时间了。当我运行单独的 Thread
/ AsyncTask
时,如何正确处理屏幕方向更改?目前,我
android:configChanges="orientation|keyboard|keyboardHidden"
的 AndroidManifest.xml
中有,但这是 并不真正鼓励:
注意:应避免使用此属性,并且仅将其用作最后的手段。请阅读处理运行时更改,了解有关如何正确处理由于配置更改而重新启动的更多信息。
另外,在2.3模拟器中,切换到横向
时它可以工作,但切换回纵向
时会失败。
现在,我使用 configChanges 的原因是,当用户切换方向时,我可能会运行一个 AsyncTask,进行一些网络流量,并且我不希望它停止。
有没有其他方法可以做到这一点,或者有没有办法修复 2.3 以切换回纵向?
我了解 onRetainNonConfigurationInstance
,但我不确定“保存”AsyncTask
实例是否是一个好主意,主要是因为扩展 AsyncTask< 的类/code> 不是静态的(因此它与
Activity
绑定)——而且它需要是静态的,因为在 onPostExecute()
中它调用来自 的方法Activity
实例。
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"
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.
Also, in the 2.3 emulator, it works when switching to landscape
, but switching back to portrait
fails.
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.
Is there any other way of doing this, or is there a way of fixing 2.3 to switch back to portrait?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了与您类似的问题,并通过将
AsyncTask
实现为继承自Application
类。Application
类在应用程序的整个生命周期内都可用,因此您不必担心AsyncTask
被中断,除非整个应用程序将被终止。要在任务完成时收到通知,
Activity
必须实现一个接口,用于将自身注册到Application
类。当您的应用程序因屏幕旋转而被销毁时,您可以从
Application
类中取消注册您的Activity
并在重新创建时重新注册。如果任务在销毁和重新创建之间完成,则操作结果可以同时存储在Application
类中,以便Activity
可以检查任务是否仍在运行或结果是否重新创建时已经可用。另一个优点是您可以直接访问应用程序上下文,因为
Application
类是Context
类的子类。I had a similar problem to your and worked around it by implementing the
AsyncTask
as part of a class which inherits fromApplication
class. AnApplication
class is available all the life time of the application So you don't have to worry about yourAsyncTask
getting interrupted unless the whole application will be killed.To get notified when the task has finished the
Activity
has to implement a interface which it uses to register itself to theApplication
class.When your application is destroyed because of the screen rotation you can unregister your
Activity
from theApplication
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 theApplication
class meanwhile so theActivity
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 theContext
class.查看 droid-fu 库
BetterAsyncTask
。它旨在处理这种确切的情况。http:// Brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/
Take a look the droid-fu library
BetterAsyncTask
. It is meant to handle this exact case.http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/
我已经在此处提出了类似的问题。
基本上有一个 如何在设备轮换时暂停/恢复
AsynTask
的示例。然而,它仍然不适合所有情况(有时无法安全地暂停操作,例如在远程服务器上创建新用户)。对于那些“不安全”的情况,您需要编写一些代码,我称之为棘手的“框架”。您将看到 CommonsWare 提供了该链接的 github 链接。I already popped up similar question here.
Basically there is an example of how to pause/resume an
AsynTask
on device rotation. However it still does not fit for all cases (sometimes it is not possible to safely suspend the action, such as a new user creation on a remote server). For those "unsafe" cases you need to code somewhat I'd call a tricky "framework". You will see CommonsWare gives github links to the one.