title: Replace fragment with another fragment inside ViewPager
body: <p>I'm using a ViewPager together with a FragmentPagerAdapter to host three different fragments</p>
  
  <pre><code>[Fragment1][Fragment2][Fragment3]
  </code></pre>
  
  <p>What I'm trying to achieve is to successfully replace Fragment1 with a whole new fragment, Fragment4, if a specific task succeeds.</p>
  
  <p>When I use..</p>
  
  <pre><code>FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction transaction = fragmentManager.beginTransaction();
  transaction.replace(R.id.fragment1_layout_id, fragment4);
  transaction.commit();
  </code></pre>
  
  <p>..the fragment is replaced beautifully and Fragment4 is shown instead of Fragment1. Though as soon as I swipe all the way to Fragment3 and then back to Fragment4, Fragment1 has made a comeback.</p>
  
  <p>Then again, if I use..</p>
  
  <pre><code>FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction transaction = fragmentManager.beginTransaction();
  transaction.remove(fragment1);
  transaction.commit();
  </code></pre>
  
  <p>..Fragment1 is removed and when I come back Fragment4 is there. So the problem with this solution is that I couldn't find a way to immediately show Fragment4 as Fragment1 is removed, even if I tried:</p>
  
  <pre><code>transaction.add(fragment4);
  transaction.show(fragment4);
  </code></pre>
  
  <p>And here's how my FragmentPagerAdapter implementation looks like at the moment, <strong>without any transaction managing</strong>:</p>
  
  <pre><code>public static class PagerAdapter extends FragmentPagerAdapter {
  
      public PagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public int getCount() {
          return VIEW_COUNT;
      }
  
      @Override
      public Fragment getItem(int position) {
  
          Fragment fragment = null;
  
          switch (position) {
          case 0:
              fragment = Fragment1.newInstance(context_);
              break;
  
          case 1:
              fragment = Fragment2.newInstance(context_);
              break;
  
          case 2:
              fragment = Fragment3.newInstance(context_);
              break;
  
          default:
              break;
          }
  
          return fragment;
      }
  }
  </code></pre>
  
  <p>Edit.</p>
  
  <p>So it seems like I wasn't totally clear with my question. I decided to remove all the spaghetti I managed to create earlier and tried to state that I had left them off (see bolded text above).</p>
  
  <p>Anyway here's pretty much what I have been trying to do inside getItem():</p>
  
  <pre><code>@Override
  public Fragment getItem(int position) {
  
      Fragment fragment = null;
  
      switch (position) {
      case 0:
  
          if (!app.isUserLoggedIn) {
              if (loginFragment_ == null) {
                  loginFragment_ = LoginFragment.newInstance(context_);
                  transaction_ = fragmentManager_.beginTransaction();
  
                  if (logoutFragment_ != null) {
                      transaction_.remove(logoutFragment_);
                      logoutFragment_ = null;
                  }
  
                  transaction_.add(loginFragment_, "login");
                  transaction_.commit();
              }
  
              if (fragmentManager_.findFragmentByTag("login") == null)
                  fragment = LoginFragment.newInstance(context_);
              else
                  fragment = fragmentManager_.findFragmentByTag("login");
  
          } else {
              if (logoutFragment_ == null) {                      
                  logoutFragment_ = LogoutFragment.newInstance(context_);
                  transaction_ = fragmentManager_.beginTransaction();
  
                  if (loginFragment_ != null) {
                      transaction_.remove(loginFragment_);
                      loginFragment_ = null;
                  }
  
                  transaction_.add(logoutFragment_, "logout");
                  transaction_.commit();
              }
  
              if (fragmentManager_.findFragmentByTag("logout") == null)
                  fragment = LogoutFragment.newInstance(context_);
              else
                  fragment = fragmentManager_.findFragmentByTag("logout");
          }
          break;
  
      case 1:
          fragment = HomeFragment.newInstance(context_);
          break;
  
      case 2:
          fragment = RegisterFragment.newInstance(context_);
          break;
  
      default:
          break;
      }
  
      return fragment;
  }
  </code></pre>
  
  <p>With this code I get nothing done, but if someone sees what I'm doing wrong and would like to point me in the correct direction, I'd appreciate it a lot!</p>