当 AsyncTasks 运行时处理屏幕方向变化

发布于 2024-10-10 02:46:22 字数 956 浏览 0 评论 0原文

我已经被这个困扰有一段时间了。当我运行单独的 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 技术交流群。

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

发布评论

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

评论(3

风渺 2024-10-17 02:46:22

我遇到了与您类似的问题,并通过将 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 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.

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.

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.

做个少女永远怀春 2024-10-17 02:46:22

我已经在此处提出了类似的问题。

基本上有一个 如何在设备轮换时暂停/恢复 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.

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