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

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

问题描述

我感谢大量关于轮换更改的 AsyncTask 的帖子.使用兼容性库并尝试关闭 onPostExecute 中的 DialogFragment 时,我遇到以下问题.

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.

我有一个片段,它会触发一个显示进度的 AsyncTask,DialogFragment,然后在 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 因为我正在尝试在 Activity 保存其状态时有效地提交事务并且我理解这一点.

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:addFragmentTransaction:commitAllowingStateLoss 来显示对话框,但这并非没有问题.

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

推荐答案

如果你需要将你的任务与 Activity 生命周期同步,我相信 Loaders 正是您所需要的.更具体地说,您应该使用 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.

还有另一种方法来处理这个任务:使用 保留其实例.一般的想法是创建一个没有 UI 的片段并调用 setRetainInstance(true).它有一个任务,它会收到有关活动是否可用的通知.如果没有,任务的线程将挂起,直到有活动可用.

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天全站免登陆