title: Dealing with fragment and view pager : can't change tag of fragment AND Recursive entry to executePendingTransactions
body: <p>Firstly, I know these subjects have been created a lot of time on stackoverflow, but I don't have found the solution to my problems. Secondly, I'm french, so my english is not perfect, sorry per advance and tell me if you don't understand something. And to finish this introduction, it's the first time that I'm dealing with fragments, so, sorry if there is something that I don't have well understand !</p>
  
  <p>I have three buttons, that allow to switch between three fragments.
  Inside one of these fragments, I have a view pager with two fragments. For the moment, each fragments (there are 5), only contains a TextView.
  I'm using the latest version of android-support-v4 (I have read a lot of subject in stackoverflow that say that the latest version of support solve the "Recursive entry to executePendingTransactions" error that I have).</p>
  
  <p>My two problems :</p>
  
  <ul>
  <li><p>When I click two times in one button, I have an IllegaleStateException "can't change tag of fragment". I was able to fix that by creating a new fragment on onButtonSelected method, but I don't want to recreate fragment each time, for memory reasons and for functional reasons : fragment have to keep her state. This problem is not my main problem, indeed, i know that to disable the button when user is already on fragment is possible, but it's strange to have an exception when this management is not done, no ?.</p></li>
  <li><p>When I go out from the fragment with the view pager, and I go back to this fragment, I have an IllegalStateException "Recursive entry to executePendingTransactions". I can fix this by setting my adapter on an handler, or use FragmentStatePagerAdapter instead of FragmentPageAdapter (see <a href="https://stackoverflow.com/questions/7700226/display-fragment-viewpager-within-a-fragment">Display fragment viewpager within a fragment</a>), but even if my application don't crash, when I go back to my fragment with the view pager, the view pager has disapear ! </p></li>
  </ul>
  
  <p>Can you help me ?</p>
  
  <p>Java source code is bellow, layout source code is, I think, useless.</p>
  
  <p>MainActivity :</p>
  
  <pre><code>public class MainActivity extends SherlockFragmentActivity{
  
  private SherlockFragment fragmentOne, fragmentTwo, fragmentThree;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  //      this.fragmentOne = new fragmentOne();
  //      this.fragmentTwo = new fragmentTwo();
  //      this.fragmentThree = new fragmentThree();
  // Call new or instanciate ? What's the correct way ?
  
      this.fragmentOne = (SherlockFragment) SherlockFragment.instantiate(this, FragmentOne.class.getName());
      this.fragmentTwo = (SherlockFragment) SherlockFragment.instantiate(this, FragmentTwo.class.getName());
      this.fragmentThree = (SherlockFragment) SherlockFragment.instantiate(this, FragmentThree.class.getName());
  
      // Add fragment
      FragmentTransaction transaction = (
          this.getSupportFragmentManager().beginTransaction()
      );      
      transaction.add(
          R.id.tab_fragment,
          this.fragmentOne,
          this.fragmentOne.toString()
      );
      transaction.commit();
  
  }
  
  public void onButtonSelected(View v){
  
      switch (v.getId()){
  
          case R.id.button_one_tab:{
              showFragment(this.fragmentThree);
              break;
          }
          case R.id.button_two_tab:{
              showFragment(this.fragmentOne);
              break;
          }
          case R.id.button_three_tab:{
              showFragment(this.fragmentTwo);
              break;
          }
          default:{
              break;
          }
  
      }
  
  }
  
  public void showFragment(SherlockFragment fragmentToShow){
  
      FragmentTransaction transaction = (
          this.getSupportFragmentManager().beginTransaction()
      );
  
      transaction.replace(R.id.tab_fragment, fragmentToShow, fragmentToShow.toString());
      transaction.commit();
  
  }
  
  }
  </code></pre>
  
  <p>Fragment two and three only inflate a layout that only contains a TextView.
  Fragment one (note that i'm using a DirectionalViewPager - a lib - instead of a ViewPager):</p>
  
  <pre><code>public class FragmentOne extends SherlockFragment{
  
  private FragmentOneAdapter fragmentOneAdapter;
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
  
      super.onActivityCreated(savedInstanceState);
  
      // Set up the pager
      final DirectionalViewPager pager = (DirectionalViewPager)getActivity().findViewById(R.id.pager);
      if (this.fragmentOneAdapter== null){
          this.fragmentOneAdapter= new FragmentOneAdapter (getActivity().getSupportFragmentManager());
      }
      pager.setAdapter(fragmentOneAdapter);
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return inflater.inflate(R.layout.fragment_landing_page, container, false);
  }
  </code></pre>
  
  <p>FragmentOneAdapter :</p>
  
  <pre><code>public class FragmentOneAdapter extends FragmentPagerAdapter{
  
  private ArrayList&lt;SherlockFragment&gt; fragmentsList;
  
  public FragmentOneAdapter(FragmentManager fragmentManager) {
  
      super(fragmentManager);
  
      SherlockFragment fragmentFour = new FragmentFour();
      SherlockFragment fragmentFive = new FragmentFive();
  
      this.fragmentsList = new ArrayList&lt;SherlockFragment&gt;();
      this.fragmentsList.add(fragmentFour);
      this.fragmentsList.add(fragmentFive);
  
  }
  
  @Override
  public Fragment getItem(int position) {
      return this.fragmentsList.get(position);
  }
  
  @Override
  public int getCount() {
      return this.fragmentsList.size();
  }
  
  }
  </code></pre>
  
  <p>Thanks per advance for your help !</p>