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
.
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
in which I try to dismiss and invoke a dialog.
I get an IllegalStateException
because I'm trying to effectively commit a transaction when the activity has saved its state and I understand this.
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.
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.
Thanks in advance, peter.
Edit 1
Need to support 2.1 and above
Edit 2
I have considered showing the dialog using FragmentTransaction:add
and FragmentTransaction:commitAllowingStateLoss
however this isn't without its problems.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…