title: How to update Fragment Pager Adapter from Fragments?
body: <p><strong>Problem ::</strong> </p>
  
  <p>I have one SherlockFragmentActivity in which i have added six fragments.
  Now out of six 3 fragments are added based on the conditions.</p>
  
  <p><strong>loading the fragments ::</strong> </p>
  
  <pre><code>private void loadFragments()
  {
      try
      {
          dataHelper=new DataHelper(this);
          menuInfo=dataHelper.getTransMenuInfo();
  
          servicesName = new ArrayList&lt;String&gt;();
          servicesImage = new ArrayList&lt;Integer&gt;();
  
          if(menuInfo.contains("1"))
          {
              servicesName.add(dataHelper.getTransMenu_ModuleName("1"));
              servicesImage.add(R.drawable.icon);
          }
          if(menuInfo.contains("2"))
          {
              servicesName.add(dataHelper.getTransMenu_ModuleName("2"));
              servicesImage.add(R.drawable.icon);
          }
          if(menuInfo.contains("4"))
          {
              servicesName.add(dataHelper.getTransMenu_ModuleName("4"));
              servicesImage.add(R.drawable.icon);
          }
  
          dataHelper.close();
          servicesName.add("Fragment 4");
          servicesImage.add(R.drawable.wallet);
  
          servicesName.add("Fragment 5");
          servicesImage.add(R.drawable.reports);
  
          servicesName.add("Fragment 6");
          servicesImage.add(R.drawable.setting);
  
          List&lt;Fragment&gt; fragments = getFragments();
  
          if(fragments!=null &amp;&amp; fragments.size()&gt;0)
          {
              actionbartabmenuAdapter = new ActionBarTabMenuAdapter(getSupportFragmentManager(), fragments,this,servicesName,servicesImage);
              awesomePager.setAdapter(actionbartabmenuAdapter);
          }
      }
      catch(SQLiteException e)
      {
          e.printStackTrace();
      }
      catch(NullPointerException e)
      {
          e.printStackTrace();
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p><strong>Adapter ::</strong></p>
  
  <pre><code>private class ActionBarTabMenuAdapter extends FragmentPagerAdapter  
  {
      Activity context;
      Context ctx;
      ArrayList&lt;String&gt; menuInfo;
      private List&lt;Fragment&gt; fragments;
      ArrayList&lt;String&gt; services;
      ArrayList&lt;Integer&gt; images;
  
      public ActionBarTabMenuAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments,Context ctx,ArrayList&lt;String&gt; servicesName,ArrayList&lt;Integer&gt; servicesImage)
      {
          super(fm);
          this.context=(Activity) ctx;
          dataHelper=new DataHelper(ctx);
          menuInfo=dataHelper.getTransMenuInfo();
          dataHelper.close();
  
          this.services = servicesName;
          this.images = servicesImage;
          this.fragments = fragments;
          menuInfo.add("Wallet To Wallet");
          menuInfo.add("Reports");
          menuInfo.add("Settings");
      }
  
      @Override
      public int getCount() 
      {
          return fragments.size();
      }
      @Override
      public Fragment getItem(int position) 
      {
          return this.fragments.get(position);
      }
  }
  
  class ViewHolder {
      TextView Title, Description, ReadMore;
  }
  </code></pre>
  
  <p>In last fragment(i.e. Fragment 6)in which i have one option for updation,now on click of that button i am removing my local database and get the new value from server and store it in my local database.</p>
  
  <p>So before loading the fragments again,i am simply checking the conditions again and create the fragment list accordingly.so basically its not compulsory that fragment will be loaded 6 every time,it can be 3,4 or 5 also.</p>
  
  <p><strong>What i want ::</strong></p>
  
  <p>So i want to add or remove the fragment in view pager inside the fragment.On click of button,if i get the list 4 fragment then it should be 4 display that no. of fragment at that time(by onSwipe or onTabChanged).</p>
  
  <p><strong>What i have tried ?? ::</strong></p>
  
  <p>To achieve this, i have created one Interface with method <code>loadMenu()</code> like this :: </p>
  
  <pre><code>MenuInterface mInterface=new MenuInterface() {
      @Override
      public void loadMenu() {
          loadFragments();
      }
  };
  </code></pre>
  
  <p>Now in the Fragment 6 i have passed the object of Interface from TestActivity(from where i am setting the adapter) </p>
  
  <pre><code>fList.add(SettingsListFragment.newInstance(this,"Settings",extraMessage,mInterface));
  </code></pre>
  
  <p>Now in Fragment 6's new InstanceMethod i am getting the object and call the <code>loadMenu()</code> method.</p>
  
  <p>But now after applying this mechanism i am getting the following exception ....</p>
  
  <p><strong>Error ::</strong> </p>
  
  <pre><code>java.lang.IllegalStateException: Must be called from main thread of process
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1392)
  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
  at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:321)
  at com.example.testapp.TestActivity.loadFragments(TestActivity.java:238)
  com.example.testapp.TestActivity.access$0(TestActivity.java:193)
  at v.ButtonPayActivity$1.loadTransactionMenu(TestActivity.java:596)
  at com.example.testapp.home.SettingsListFragment$4.run(SettingsListFragment.java:769)
  at java.lang.Thread.run(Thread.java:1019)
  </code></pre>
  
  <p><strong>EDIT ::</strong></p>
  
  <pre><code>MenuInterface mMenuInterface=new MenuInterface() {
          @Override
          public void loadTransactionMenu() {
              runOnUiThread(new Runnable() {
                  public void run() {
                      loadFragments();
                      if(actionbartabmenuAdapter!=null){
                          actionbartabmenuAdapter.notifyDataSetChanged();
                      }
                  }
              });
          }
      };
  </code></pre>
  
  <p><strong>Adapter ::</strong> </p>
  
  <pre><code>private class ActionBarTabMenuAdapter extends FragmentStatePagerAdapter  
  {
      Activity context;
      Context ctx;
      ArrayList&lt;String&gt; menuInfo;
      private List&lt;Fragment&gt; fragments;
      ArrayList&lt;String&gt; services;
      ArrayList&lt;Integer&gt; images;
  
      public ActionBarTabMenuAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments,Context ctx,ArrayList&lt;String&gt; servicesName,ArrayList&lt;Integer&gt; servicesImage)
      {
          super(fm);
          this.context=(Activity) ctx;
          dataHelper=new DataHelper(ctx);
          menuInfo=dataHelper.getTransMenuInfo();
          dataHelper.close();
  
          this.services = servicesName;
          this.images = servicesImage;
          this.fragments = fragments;
          menuInfo.add("Wallet To Wallet");
          menuInfo.add("Reports");
          menuInfo.add("Settings");
      }
  
      @Override
      public int getCount() 
      {
          return fragments.size();
      }
       public int getItemPosition(Object item) {
          //Fragment fragment = (Fragment)item;
          return POSITION_NONE;
      }
  
      @Override
      public Fragment getItem(int position) 
      {
          return this.fragments.get(position);
      }
  }
  
  class ViewHolder {
      TextView Title, Description, ReadMore;
  }
  </code></pre>