title: Adding a page to Fragment Page Adapter dynamically
body: <p>Im new to Android, Was using FragmentPageAdapter which works perfectly fine when used like the following</p>
  
  <pre><code> public Fragment getItem(int position) {
  
          if (position == 0) {
                  MenuItemFragment frag = new MenuItemFragment();
                  Bundle bundle = new Bundle();
                  bundle.putInt("pos", position);
                  frag.setArguments(bundle);
                  return frag;
              } else if (position == 1) {
                  MenuItemFragment1 frag = new MenuItemFragment1();
                  Bundle bundle = new Bundle();
                  bundle.putInt("pos", position);
                  frag.setArguments(bundle);
                  return frag;
              } else if (position == 2) {
                  MenuItemFragment2 frag = new MenuItemFragment2();
                  Bundle bundle = new Bundle();
                  bundle.putInt("pos", position);
                  frag.setArguments(bundle);
                  return frag;
              }
              return null;
          }    
  </code></pre>
  
  <p>So in the above code i need to inflate MenuItemFragment for the corresponding position.</p>
  
  <p>My question is: Is it possible to add Fragments dynamically depending on some condidtion. Here all my fragments have the same layout but data changes for every Fragment.</p>
  
  <p><em><strong>What i did for the above code to return fragments dynamically. Is as follows.</em></strong></p>
  
  <pre><code>public Fragment getItem(int position) {
      if (SubMenuActivity.sMenuCategory.size() &gt; 0) {
          SubMenuActivity.sMenuCategory.clear();
      }
  
      MenuItemFragment frag = new MenuItemFragment();
      try {
          for (int j = 0; j &lt; SubMenuActivity.sMenuCard.size(); j++) {
              String category = SubMenuActivity.sMenuCard.get(j).mCategory;
              String header = SubMenuActivity.sMenuFragHeader[position];
  
              if (category.equalsIgnoreCase(header)) {
                  SubMenuActivity.sMenuCategory.add(SubMenuActivity.sMenuCard
                          .get(j));
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      return frag;
  
  }
  </code></pre>
  
  <p>The Problem here is that for each position i'm adding data to sMenuCategory.But here getItem(position) loops twice for the first time and clears the data added when position is zero, and retains the data added when position is one and displays the same data for the first two fragments.</p>
  
  <p>Please help me solve this issue. of clearing the arrayList of every position so as to contain data as per the list not repeated.</p>