如何处理的AsyncTask onPostExecute暂停时以避免IllegalStateException异常 [英] How to handle AsyncTask onPostExecute when paused to avoid IllegalStateException

查看:172
本文介绍了如何处理的AsyncTask onPostExecute暂停时以避免IllegalStateException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我AP preciate有关AsyncTask的一个旋转变化的众多帖子。我已经使用了兼容性lib和试图解雇时,下面的问题一个 DialogFragment onPostExecute

I appreciate the numerous postings regarding AsyncTask on a rotation change. I have the following problem when using the compatability lib and trying to dismiss a DialogFragment in onPostExecute.

我有一个片段,它会显示进度 DialogFragment 的的AsyncTask的火,然后在 onPostExecute 关闭对话框然后有可能抛出了另一个 DialogFragment

I have a fragment which fires of an AsyncTask which displays a progress DialogFragment, then in onPostExecute dismisses the dialog and then potentially throws up another DialogFragment.

如果在显示进度对话框,当我把申请到我碰到下面的我的片段背景:

If when the progress dialog is being displayed I put the application into the background I get the following for my fragment:

1)的onPause

2)的onSaveInstanceState

3) onPostExecute 中,我试图解雇并调用一个对话框。

3) onPostExecute in which I try to dismiss and invoke a dialog.

我得到一个 IllegalStateException异常,因为我想有效地提交事务时,活动已保存的状态,我理解这一点。

I get an IllegalStateException because I'm trying to effectively commit a transaction when the activity has saved its state and I understand this.

在旋转,我认为(也许是错误的),我不会得到一个 onPostExecute ,直到活动已重新创建。然而,把应用程序的时候到我假设(绝对不正确地)的 onPostExectute 将不会被调用,而该片段/活动暂停的背景。

On a rotation I've assumed (perhaps incorrectly) that I wouldn't get an onPostExecute until the activity has been recreated. However, when putting the application into the background I assumed (definitely incorrectly) that the onPostExectute wouldn't get called while the fragment/activity was paused.

我的问题是,是我的解决方案,只需在 onPostExecute 检测该片段/活动暂停,简单地执行了我需要做的, onResume 呢?似乎有点丑陋的我。

My question is, is my solution to simply detect in onPostExecute that the fragment/activity is paused and simply perform what I need to do in onResume instead? Seems somewhat ugly to me.

在此先感谢,彼得。

修改1

需要支持2.1及以上

编辑2

我已经考虑显示使用 FragmentTransaction对话框:添加 FragmentTransaction:commitAllowingStateLoss 然而,这并不是没有的问题。

I have considered showing the dialog using FragmentTransaction:add and FragmentTransaction:commitAllowingStateLosshowever this isn't without its problems.

推荐答案

如果您需要与该活动周期同步你的任务,我相信的Loaders 正是你所需要的。更具体地讲,你应该使用<一个href="http://developer.android.com/reference/android/content/AsyncTaskLoader.html">AsyncTaskLoader做这项工作。而不是运行的AsyncTask所以,现在,启动加载程序,然后等待响应的监听器。如果活动被暂停,你不会得到一个回调,这部分会为你进行管理。

If you need to synchronize your task with the activity lifecycle, I believe that Loaders are exactly what you need. More specifically, you should use AsyncTaskLoader to do the job. So now instead of running an AsyncTask, you launch your loader, then wait for response in a listener. If the activity is paused, you won't get a callback, this part will be managed for you.

还有另一种方式来处理这个任务:使用片段<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.html">retains它的实例的。总的想法是您创建一个片段,没有用户界面,并称之为 setRetainInstance(真)。它有它想得到有关的活动是可用与否的任务。如果不是,则任务的线程挂起直至一个活动变得可用。

There is another way to handle this task: using a fragment which retains its instance. The general idea is that you create a fragment without UI and call setRetainInstance(true). It has a task which is being notified about the activity being available or not. If not, the task's thread suspends until an activity becomes available.

这篇关于如何处理的AsyncTask onPostExecute暂停时以避免IllegalStateException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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