How to handle AsyncTask onPostExecute when paused to avoid IllegalStateException

后端 未结 5 770
灰色年华
灰色年华 2020-11-30 18:44

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 Dial

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 19:34

    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.

    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.

提交回复
热议问题