title: Weird issue refreshing ViewPager with new content:
body: <p>I receive data from a web service that is parsed into an object in the <code>Application</code> class, this object which in my case is a <code>Report</code> class contains a list of <code>Tabs</code> that should populate the <code>ViewPager</code>. So this Report is stored in the <code>currentReport</code> parameter in the Application class.</p>
  
  <p>Each tab should be a page in the <code>ViewPager</code>. So for the first time when I receive the data and parse it, all works perfectly and the View Pager is populated with the correct data and presents it.</p>
  
  <p>My problem starts when I want to get a new <code>Report</code> from the server and refresh the <code>ViewPager</code> with the new data that was parsed into the <code>currentReport</code> parameter.</p>
  
  <p>My <code>ViewPager</code> is swipe-able and has tabs navigation, So when I try to re-instantiate the <code>ViewPager</code> with the new <code>Report</code> I see the right amount of tabs added to the <code>tabHost</code>. for some reason the fragments of the new report overlaping the fragment of the previos one. So if the number of tabs in the new report is bigger the tabs in the previous report then I can see the tabs from the previous report in addition to part of the new ones (if for example in the first report I had one tab, and in the new one I have 3 then I see the one fragment from the previous and the last 2 from the next one). </p>
  
  <p>I have started my development from this tutorial and added smoe code to it:
  <a href="http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/" rel="nofollow noreferrer">http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/</a></p>
  
  <p>This is my Tab Activity code:</p>
  
  <pre><code>public class TabsViewPagerFragmentActivity extends FragmentActivity implements    ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener 
  {
  static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
  private TabHost mTabHost;
  private ViewPager mViewPager;
  private HashMap&lt;String, TabInfo&gt; mapTabInfo;
  public ViewPagerAdapter mPagerAdapter;
  private TextView tvReportName, tvTabTitle;
  private Button bBackToParameters;
  private Dialog progressDialog;
  private SGRaportManagerAppObj application;
  private int numberOfTabs = 0;
  private Display display;
  public static final int POPUP_MARGIN = 6;
  LeftSideMenu leftSideMenu;
  
  public void NotifyTabActivityViewPagerAdapter()
  {
      mPagerAdapter.notifyDataSetChanged();
  }
  
  public ViewPagerAdapter getTabActivityViewPagerAdapter()
  {
      return mPagerAdapter;
  }
  
  public ViewPager getTabActivityViewPager()
  {
      return mViewPager;
  }
  
  public void setCurrentTabTitle (String title)
  {
      tvTabTitle.setText(title);
      Log.d(TAG, "set tab title from activity: "+title);
  }
  
  
  /**
  * Maintains extrinsic info of a tab's construct
  */
  private class TabInfo 
  {
      private String tag;
      private Class&lt;?&gt; clss;
      private Bundle args;
      private Fragment fragment;
  
      TabInfo(String tag, Class&lt;?&gt; clazz, Bundle args) 
      {
          this.tag = tag;
          this.clss = clazz;
          this.args = args;
      }
  }
  
  /**
   * A simple factory that returns dummy views to the Tabhost
   */
  class TabFactory implements TabContentFactory {
  
      private final Context mContext;
  
      /**
       * @param context
       */
      public TabFactory(Context context) {
          mContext = context;
      }
  
      /** (non-Javadoc)
       * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
       */
      public View createTabContent(String tag) {
          View v = new View(mContext);
          v.setMinimumWidth(0);
          v.setMinimumHeight(0);
          return v;
      }
  }
  
  /** (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
  */
  public void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      application = SGRaportManagerAppObj.getInstance();
      display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
      // Inflate the layout
      setContentView(R.layout.tabs_screen_activity_layout);
      tvTabTitle = (TextView) findViewById(R.id.tvTabName);
      tvReportName = (TextView)findViewById(R.id.tvReportName);
      tvReportName.setText(application.currentReport.getName()+ " - ");
      bBackToParameters = (Button) findViewById(R.id.bBackToParameters);
      leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
      applyOnClickListenerToLeftSideMenu();
  
      findViewById(R.id.showLeftMenuButton).setOnClickListener(new View.OnClickListener() 
      {
          @Override
          public void onClick(View v) 
          {               
              Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
              int width = d.getWidth();
  
              View panel = findViewById(R.id.leftSideMenu);
              View appPanel = findViewById(R.id.appLayout);
              if (panel.getVisibility() == View.GONE){
                  appPanel.setLayoutParams(new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT));
                  panel.setVisibility(View.VISIBLE);
                  applyOnClickListenerToLeftSideMenu();
              }else{
                  ToggleButton button = (ToggleButton) findViewById(R.id.showLeftMenuButton);
                  button.setChecked(false);
                  panel.setVisibility(View.GONE);
              }
          }
      });
  
      // Initialise the TabHost
      progressDialog = DialogUtils.createProgressDialog(this, this.getString(R.string.populating_view_pager));
      progressDialog.show();
  
      if (SGRaportManagerAppObj.getInstance().parametersRepository.getParametersRepository().size() == 0)
      {
          bBackToParameters.setText(R.string.back_to_report_list);
      }
      this.initialiseTabHost(savedInstanceState);
  
      if (savedInstanceState != null) {
          mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
      }
      // Intialise ViewPager
      this.intialiseViewPager();
      progressDialog.dismiss();
  }
  
   /** (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
   */
  protected void onSaveInstanceState(Bundle outState) {
      outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
      super.onSaveInstanceState(outState);
  }
  
  /**
   * Initialise ViewPager
   */
  public void intialiseViewPager() 
  {
  
      List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;();
  
    // TabInfo tabInfo = null;
  
      if (application.getCurrentDataSource().equals(DataSource.SSRS))
      {
          numberOfTabs = application.currentReport.getTabsList().size();
      }
      else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
      {
          numberOfTabs = application.currentReport.getODTabsList().size();
          Log.d(TAG, "CURRENT REPORT FROM VIEW PAGER: "+ application.currentReport.toString());
      }   
  
      Log.d(TAG,"Current Tabs number from TabsViewPager activity: " +numberOfTabs);
  
      if (application.getCurrentDataSource().equals(DataSource.SSRS))
      {
           for (int i = 0; i &lt; numberOfTabs; i++)     
           {
              Tab tempTab = application.currentReport.getTabsList().get(i);
              if (tempTab.getTabTemplateId() == 7)
              {
                  GridFragment gridFragment = new GridFragment(tempTab);
                  fragments.add(gridFragment);
              }
              else  if (tempTab.getTabTemplateId() == 8)
              {
                  NewChartFragment chartFragment = new NewChartFragment(tempTab, this);
                  fragments.add(chartFragment);
              }
           }
      }
      else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
      {
           for (int i = 0; i &lt; numberOfTabs; i++)     
           {
              ODTab tempTab = application.currentReport.getODTabsList().get(i);
              if (tempTab.getTabType().equals(ODGrid.XML_GRID_ELEMENT))
              {
                  GridFragment gridFragment = GridFragment.newInstance(tempTab.getTabId());
                  fragments.add(gridFragment);
              }
              else  if (tempTab.getTabType().equals(ODChart.XML_CHART_ELEMENT))
              {
                  NewChartFragment chartFragment = NewChartFragment.newInstance(tempTab.getTabId());
                  fragments.add(chartFragment);
              }
           }
      }   
  
      Log.d(TAG, "Current report fragments set to adapter: "+fragments.toString());
     /*
      if (this.mPagerAdapter == null)
      {
          this.mPagerAdapter  = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
      }
      else
      {
          this.mPagerAdapter.removeAllFragments();
          this.mPagerAdapter.addFragmentsListToAdapter(fragments);
      }
      */
      this.mPagerAdapter  = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
      this.mViewPager = (ViewPager)super.findViewById(R.id.pager);
  //    this.mViewPager.setAdapter(null);
      this.mViewPager.setAdapter(this.mPagerAdapter);
      this.mViewPager.setOffscreenPageLimit(0);
      this.mViewPager.setOnPageChangeListener(this);
      Log.d(TAG, "Adapter initialized!");
  }
  
  /**
   * Initialise the Tab Host
   */
  public void initialiseTabHost(Bundle args) {
      mTabHost = (TabHost)findViewById(android.R.id.tabhost);
  
      /*
      //new edit
      if (mTabHost.getChildCount() &gt; 0)
      {
          mTabHost.removeAllViews();
      }
      */
  
      mTabHost.setup();
      TabInfo tabInfo = null;
      mapTabInfo = new HashMap&lt;String, TabsViewPagerFragmentActivity.TabInfo&gt;();
      if (args != null)
      {}
      else
      {
          if (application.getCurrentDataSource().equals(DataSource.SSRS))
          {
              int numberOfTabs = application.currentReport.getTabsList().size();
              for (int i = 0; i &lt; numberOfTabs; i++)      
              {
                  Tab tempTab = application.currentReport.getTabsList().get(i);
                  if (tempTab.getTabTemplateId() == 7)
                  {
                      //GridFragment gridFragment = new GridFragment(tempTab);
                      TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), GridFragment.class, args)));
                      this.mapTabInfo.put(tabInfo.tag, tabInfo);
                  }
                  else  if (tempTab.getTabTemplateId() == 8)
                  {
                      TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), NewChartFragment.class, args)));
                      this.mapTabInfo.put(tabInfo.tag, tabInfo);
                  }
              }
          }
  
          else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
          {
              int numberOfTabs = application.currentReport.getODTabsList().size();
              for (int i = 0; i &lt; numberOfTabs; i++)      
              {
                  ODTab tempTab = application.currentReport.getODTabsList().get(i);
              //  Log.d(TAG,"Crashed Tab type: "+ tempTab.getTabType());
                  if (tempTab.getTabType().equals(ODGrid.XML_GRID_ELEMENT))
                  {
                      //GridFragment gridFragment = new GridFragment(tempTab);
                      TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), GridFragment.class, args)));
                      this.mapTabInfo.put(tabInfo.tag, tabInfo);
                  }
                  else  if (tempTab.getTabType().equals(ODChart.XML_CHART_ELEMENT))
                  {
                      TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), NewChartFragment.class, args)));
                      this.mapTabInfo.put(tabInfo.tag, tabInfo);
                  }
              }
          }
      }
      // Default to first tab
      //this.onTabChanged("Tab1");
      //
      mTabHost.setOnTabChangedListener(this);
  }
  
  /**
   * Add Tab content to the Tabhost
   * @param activity
   * @param tabHost
   * @param tabSpec
   * @param clss
   * @param args
   */
  private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) 
  {
      // Attach a Tab view factory to the spec       
      ImageView indicator = new ImageView(activity.getBaseContext());
      indicator.setPadding(10, 10, 10, 10);
      indicator.setImageResource(R.drawable.tab_select_icon_selector);
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      lp.setMargins(10, 10, 10, 10);
      indicator.setLayoutParams(lp);
      tabSpec.setIndicator(indicator); 
      tabSpec.setContent(activity.new TabFactory(activity));
      tabHost.addTab(tabSpec);
  }
  
  /** (non-Javadoc)
   * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
   */
  public void onTabChanged(String tag) {
      //TabInfo newTab = this.mapTabInfo.get(tag);
      int pos = this.mTabHost.getCurrentTab();
      this.mViewPager.setCurrentItem(pos);
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
   */
  @Override
  public void onPageScrolled(int position, float positionOffset,
          int positionOffsetPixels) {
      // TODO Auto-generated method stub
  
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
   */
  @Override
  public void onPageSelected(int position) {
      // TODO Auto-generated method stub
      this.mTabHost.setCurrentTab(position);
  }
  
  
  /* (non-Javadoc)
   * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
   */
  @Override
  public void onPageScrollStateChanged(int state) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>and this is my Adapter code:</p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter 
  {
  private List&lt;Fragment&gt; fragments;
  
  /**
   * @param fm
   * @param fragments
   */
  public ViewPagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
      super(fm);
      this.fragments = fragments;
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
   */
  
  @Override
  public Fragment getItem(int position) {
      return this.fragments.get(position);
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.view.PagerAdapter#getCount()
   */
  
  @Override
  public int getCount() {
      return this.fragments.size();
  }
  
  @Override
  public int getItemPosition(Object object) {
      return POSITION_NONE;
  } 
  </code></pre>
  
  <p>My AsyncTask that parses the data and updated the ViewPager:</p>
  
  <pre><code>public class ParseSGRDLReportDataAsyncTask extends AsyncTask&lt;String, Object, Report&gt;{
  
  static final String TAG = ParseSGRDLReportDataAsyncTask.class.getSimpleName();
  private Activity activity;  
  private Exception exception;
  private ResponseHandler responseHandler = new ResponseHandler();
  private SGRaportManagerAppObj application;
  private Dialog progressDialog;
  
  public ParseSGRDLReportDataAsyncTask(LoginScrActivity activity) {
      super();
      this.activity = activity;
  }
  
  public ParseSGRDLReportDataAsyncTask(Activity currentActivity) {
      super();
      this.activity = currentActivity;
  }
  
  public void onPreExecute() {
      application = SGRaportManagerAppObj.getInstance();
      progressDialog = DialogUtils.createProgressDialog(activity, activity.getString(R.string.parse_data_dialog_message));
      progressDialog.show();
  }
  
  @Override
  protected Report doInBackground(String... params) {
  
      String ReportDataString = params[0];
      try{
          return DataAccessManager.parseSGRDLReportData(ReportDataString);            
      }
      catch (Exception e) {
          exception = e;
          return null;
      }
  }
  
  @Override
  public void onPostExecute(Report result) {
      progressDialog.dismiss();
  
      if (exception == null) 
      {
          if (activity.getClass() == TabsViewPagerFragmentActivity.class)
          {
              application.currentReport = result;
              Log.d(TAG, "Current report parsed is: "+result);
  
              ((TabsViewPagerFragmentActivity)activity).intialiseViewPager();
  
              ((TabsViewPagerFragmentActivity)activity).initialiseTabHost(null);
  
              ((TabsViewPagerFragmentActivity)activity).NotifyTabActivityViewPagerAdapter();
  
              }
          else
          {
              responseHandler.handleProcessSGRDLReportDataResult(result, activity);       
          }
      }
      else {
          processException();
      }
  }
  
  private void processException() {
      Toast.makeText(activity,   activity.getString(R.string.error_when_parsing_data), 3000).show();
  }
  }
  </code></pre>
  
  <p><strong>Answer:</strong>
  Following <strong>@Luksprog</strong> advice for all people that stumble on the same problem this is the final result that works for me, in the AsyncTask I needed to added this: </p>
  
  <pre><code>if (activity.getClass() == TabsViewPagerFragmentActivity.class)
  {
                  TabsViewPagerFragmentActivity tempActivity = ((TabsViewPagerFragmentActivity)activity);
                  application.currentReport = result;
                  Log.d(TAG, "Current report parsed is: "+result);
                  List&lt;Fragment&gt; frags =  tempActivity.getTabActivityViewPagerAdapter().getFragments(); // getter method in your adapter
                  FragmentTransaction ft = tempActivity.getSupportFragmentManager().beginTransaction();
                  for (int i = 0; i &lt; frags.size(); i++) 
                  {
                      ft.remove(frags.get(i));
                  }
                  ft.commit(); 
                  tempActivity.intialiseViewPager();
                  tempActivity.getTabHost().getTabWidget().removeAllViews();
                  tempActivity.initialiseTabHost(null);
                  tempActivity.NotifyTabActivityViewPagerAdapter();
  }
  </code></pre>
  
  <p>When the main two point are to remove the previous added fragments from the <code>FragmentManager</code>/<code>SupportFragmentManager</code> using a <code>FragmentTransaction</code> to show the correct set of fragments in the <code>ViewPager</code> and to remove all view from the <code>TabWidget</code> before adding new ones.</p>