It is quite common to spawn a time consuming computation thread. Later, we require to update Activity or Fragment with computation result.
All the while, I'm following the below guidelines. It works well for me till now.
AsyncTask needs to onPostExecute UI Fragment
- Use
setRetainInstance(true)UI-less fragment. - Use
setTargetFragmentandgetTargetFragmenttechnique - Please refer to https://stackoverflow.com/a/12303649/72437
AsyncTask needs to onPostExecute UI Activity
- Use
setRetainInstance(true)UI-less fragment. - Use
onAttachandonDetachto store reference toActivity. Google seems doesn't encourage usinggetActivity. http://developer.android.com/guide/components/fragments.html - Please refer to https://stackoverflow.com/a/16305029/72437
However, how about case for a class derived from View? I plan to launch AsyncTask from the custom View. However, how can I onPostExecute back to the View?
The reason I'm asking so is, in my custom view, certain touch event will trigger it to redraw itself with a new bitmap. Generating the new bitmap is time consuming. Hence, I plan to launch a AsyncTask, to generate such bitmap, and pass back to custom View. However, configuration change might cause custom View to be recreated. Hence, I need to ensure my AsyncTask can have correct View reference during onPostExecute.