Execute AsyncTask in a headless Fragment
<p>I have read that executing <code>AsyncTask</code> in an <code>Activity</code> has the disadvantage that the task does not cope well with configuration changes, since the <code>AsyncTask</code>'s lifecycle is not tied on the <code>Activity</code> lifecycle. Consequently, if the task should update the UI of an Activity but in the meantime the <code>Activity</code> has been destroyed due to a configuration change, the <code>AsyncTask</code> does not know anything of the newly created <code>Activity</code> and therefore it has no chance to update it. That can lead to memory leak as well. </p>

<p>The workaround proposed it to use an headless <code>Fragment</code> (<code>Fragment</code> with no UI), just to execute the <code>AsyncTask</code>, because it can survive configuration changes. I do not comprehend though, how it could be a viable approach, since the <code>Activity</code> and its views have been destroyed in any case. Example:</p>

<pre><code>  public MyFragment extends Fragment {
        private SomeAsync myAsync = new SomeAsync();
        private WeakReference&lt;View&gt; someActivityView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }

        public void setActivityView(View View) {
            someActivityView = new WeakReference&lt;View&gt;(view);
        } 

        ...

        class SomeAsync extends AsyncTask&lt;Void, Integer, Object&gt; {

        protected Object doInBackground(Void... args) {
         ...
        }

        protected void onPostExecute(Object result) {
             doUpdate(someActivityView, result);
         }

         }
       }
</code></pre>

<p>The <code>someActivityView</code> is a <code>View</code> which belongs to the <code>Activity</code> and it is set to the <code>Fragment</code> to be updated. 
I think the <code>someActivityView</code> is not preserved in case of a configuration change while the <code>Activity</code> is created again, hence what is the benefit of using a headless <code>Fragment</code> in this case? </p>