title: View overlap after updating fragment with Asynctask
body: <p>What i pretend is to display an indeterminate progress bar (for the moment is only an dialog for test purposes) while my second fragment is getting data and constructing the view without freezing the UI. </p>
  
  <p>What i've tried:</p>
  
  <ul>
  <li>AsyncTask</li>
  <li>Loader</li>
  <li>Loader with AsyncTaskLoader</li>
  <li>Adapter (not tried but maybe its an option but i don't know how to
  use an adapter with a custom layout)</li>
  </ul>
  
  <p>With the asynctask approach almost everything is right, but onPostExecute I've to update the fragment to do this i created an interface and in the main activity i remove the view and add the new one, but with this i created another problem my back stack is messed up so i'am out of options.</p>
  
  <p>A diagram to help understand how it works:
  <img src="https://i.stack.imgur.com/NKaw7.png" alt="enter image description here"></p>
  
  <p>My fragment 2:</p>
  
  <pre><code>public class FragTopics extends Fragment {
  
  Object course;
  ManSession session;
  String courseId;
  Long topicId;
  String courseName;
  private LinearLayout mainLayout;
  private ScrollView contentScrollable;
  private LinearLayout contentsLayout;
  private View myView;
  
  public FragTopics() {
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      session = new ManSession(getActivity().getApplicationContext());
  
      courseId = getArguments().getString("courseId");
      topicId = Long.parseLong(getArguments().getString("topicId"));
      courseName = getArguments().getString("courseName");
  
      new HeavyWork().execute();
  
      // Create empty view because i need to return something
      myView = new View(getActivity());
      return myView;
  }
  
  private class HeavyWork extends AsyncTask&lt;Void, Void, Void&gt; {
      private ProgressDialog dialog;
      final FragmentUpdater activity = (FragmentUpdater) getActivity();
  
      // Do the long-running work in here
      @Override
      protected Void doInBackground(Void... params) {
  
          MoodleCourseContent[] courseTopics = new ManContents(getActivity()
                  .getApplicationContext()).getContent(courseId);
  
          MoodleCourseContent singleTopic = new ManContents(getActivity()
                  .getApplicationContext()).getTopic(topicId, courseTopics);
  
          // This createTopics call another methods from the fragment class to
          // get the data and create views
          myView = createTopics(singleTopic, courseName, courseId, topicId);
          return null;
  
      }
  
      protected void onPreExecute() {
          dialog = new ProgressDialog(getActivity());
          dialog.show();
      }
  
      // This is called when doInBackground() is finished
      @Override
      protected void onPostExecute(Void ignore) {
          activity.updater(myView);
          dialog.dismiss();
      }
  
  }
  </code></pre>
  
  <p>My interface:</p>
  
  <pre><code>    public interface FragmentUpdater {
  
      public void updater(View param);
  
  }
  </code></pre>
  
  <p>My main activity where the interface is implemented:</p>
  
  <pre><code>@Override
      public void updater(View param) {
           ViewGroup vg = (ViewGroup) findViewById (R.id.main_content);
           vg.removeAllViews();
           vg.addView(param);
      }
  </code></pre>
  
  <p>My layouts are created programmatic and this is structure:</p>
  
  <pre><code>-&gt;LinearLayout
    -&gt;TextView
    -&gt;ScrollView
      -&gt;LinearLayout 
  </code></pre>
  
  <p><strong>My main question is how to display a progress bar while fragment 2 is initialized without freezing the UI and if my above approach is correct, how to update the view in the fragment after the heavyWork is done in other words after asyncTask is done doing the background work</strong></p>
  
  <p>After remove all view and add the new one onBackpressed will overlap my view with the previous fragments views</p>