如何在屏幕旋转期间处理 AsyncTask? [英] How to handle an AsyncTask during Screen Rotation?

查看:29
本文介绍了如何在屏幕旋转期间处理 AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于如何保存我的实例状态或如何处理我的活动在屏幕旋转期间被破坏的内容.

I read a lot on how to save my instance state or how to deal with my activity getting destroyed during screen rotation.

似乎有很多可能性,但我还没有弄清楚哪一种最适合检索 AsyncTask 的结果.

There seem to be a lot of possibilities but I haven't figured out which one works best for retrieving results of an AsyncTask.

我有一些 AsyncTasks 只是再次启动并调用活动的 isFinishing() 方法,如果活动正在完成,它们将不会更新任何内容.

I have some AsyncTasks that are simply started again and call the isFinishing() method of the activity and if the activity is finishing they wont update anything.

问题是我有一个 Task 向 Web 服务发出请求,该请求可能失败或成功,重新启动该任务会导致用户遭受经济损失.

The problem is that I have one Task that does a request to a web service that can fail or succeed and restarting the task would result in a financial loss for the user.

你会如何解决这个问题?可能的解决方案有哪些优点或缺点?

How would you solve this? What are the advantages or disadvantages of the possible solutions?

推荐答案

我的第一个建议是确保您确实需要在屏幕旋转时重置您的活动(默认行为).每次我遇到轮换问题时,我都会将此属性添加到 AndroidManifest.xml 中的 <activity> 标记中,并且效果很好.

My first suggestion would be to make sure you actually need your activity to be reset on a screen rotation (the default behavior). Every time I've had issues with rotation I've added this attribute to my <activity> tag in the AndroidManifest.xml, and been just fine.

android:configChanges="keyboardHidden|orientation"

它看起来很奇怪,但是它会传递给您的 onConfigurationChanged() 方法,如果您不提供它,它只会重新测量布局,这似乎在大多数情况下是处理轮换的完美方式.

It looks weird, but what it does it hand off to your onConfigurationChanged() method, if you don't supply one it just does nothing other than re-measure the layout, which seems to be a perfectly adequate way of handling the rotate most of the time.

这篇关于如何在屏幕旋转期间处理 AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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