如何处理在屏幕旋转的AsyncTask的?如何处理、屏幕、AsyncTask

2023-09-11 11:48:52 作者:旧情人变朋友多荒廖ゝ

我读了很多关于如何拯救我的实例状态或者如何处理屏幕旋转过程中我的活动被破坏掉了。

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.

现在的问题是,我有一个任务,做了要求,可以失败或成功,并重新启动任务将导致经济损失,为用户的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?

推荐答案

我的第一个建议是,以确保你确实需要一个屏幕旋转(默认行为),以重置您的活动。每次我已经受够了旋转问题,我已经添加了这个属性给我的<活性GT; 标记在AndroidManifest.xml,并一直蛮好

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.

 
精彩推荐