title: Android: Replacing a fragment in a view pager
body: <p>I have two fragments <code>SearchFragment</code> and <code>CreateFragment</code> in a view pager inside a activity called <code>TicketManagementActivity</code>. Now when the user presses the search button in <code>SearchFragment</code>, I want <code>SearchFragment</code> to be replaced with <code>SearchResultFragment</code>. I should then be able to swipe between <code>SeachResultFragment</code> and <code>CreateFragment</code> in the <code>ViewPager</code>. Also when I press back from <code>SearchResultFragment</code> I should go back to <code>SearchFragment</code>.</p>
  
  <p>Right now, when I press the button I get a blank screen instead of the layout of <code>SearchResultFragment</code>. When I press back I get to <code>SearchFragment</code> but now I have to click the button twice for the blank screen to come. Now after the blank screen comes after the double click, whenever I swipe to <code>CreateFragment</code> tab I get a blank screen instead of <code>CreateFragment</code> layout.</p>
  
  <p>I looked at quite a number of questions on SO but none of them seem to be working for me. Most useful seems to be the first two answers in <a href="https://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager/12155594#12155594">this question</a>, but the first answer doesn't handle the back press, nor am I able to implement it. The second answer seems very implementable but I get errors which I have mentioned below.</p>
  
  <p>My main <code>TicketManagemementActivity</code>:</p>
  
  <pre><code>public class TicketManagementActivity extends FragmentActivity implements
  ActionBar.TabListener {
  
      ViewPager viewPager;
      TabsPagerAdapter adapter;
      ActionBar actionBar;
  
      String[] tabs={"Search", "Create"};
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_ticket_management);
  
          viewPager=(ViewPager)findViewById(R.id.pager);
          actionBar=getActionBar();
          adapter=new TabsPagerAdapter(getSupportFragmentManager());
  
          viewPager.setAdapter(adapter);
          actionBar.setHomeButtonEnabled(false);
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          for(String tab_name : tabs){
              actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
          }
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
              @Override
              public void onPageSelected(int position) {
                  // on changing the page
                  // make respected tab selected
                  actionBar.setSelectedNavigationItem(position);
              }
  
              @Override
              public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
  
              @Override
              public void onPageScrollStateChanged(int arg0) {
              }
          });
  
      }
    //removed methods for menu creation and filling and placeholder fragment for brevity on SO
      @Override
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
  
          viewPager.setCurrentItem(tab.getPosition());
      }
  
      @Override
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  
  
      }
  
      @Override
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
  
  
      }
  
  }
  </code></pre>
  
  <p>My <code>activity_ticket_management.xml</code> which is layout set in onCreate of ticket management activity, just contains the viewpager</p>
  
  <pre><code>&lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p>My <code>TabsPagerAdapter</code> class extending <code>FragmentPagerAdapter</code>:</p>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter {
  
      public TabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int index) {
  
          switch (index) {
          case 0:
              // Top Rated fragment activity
              return new SearchFragment();
          case 1:
              // Games fragment activity
              return new CreateFragment();
  
          }
  
          return null;
      }
  
      @Override
      public int getCount() {
          // get item count - equal to number of tabs
          return 2;
      }
  
  }
  </code></pre>
  
  <p>Relevant part of my <code>SearchFragment</code>:</p>
  
  <pre><code>public class SearchFragment extends Fragment implements View.OnClickListener    {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_search, container, false);
          .
          .//some widget initializations
          .
          return rootView;
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.ticket_search_btn: searchSigmaTickets();
                                       break;
          }
      }
  
      public void searchSigmaTickets(){
          .
          .
          .
          .//some operations
          .
  
          new SearchAsyncTask().execute();
  
          }
      }
  
      private class SearchAsyncTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
          protected Void doInBackground(Void... params){
              .
              .//some more operation
              .
          }
          protected void onPostExecute(Void param){
              Fragment newFragment = new SearchResultFragment();
              //Here I use getFragmentManager and not getChildFragmentManager
              FragmentTransaction transaction = getFragmentManager().beginTransaction();
              //HERE I try to replace the fragment. I'm not sure what id to pass, I pass the id of the main veiwpager in ticketmanagement activity
              transaction.replace(R.id.pager, newFragment);
              transaction.addToBackStack(null);
              transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  
              transaction.commit();
          }
  
      }
  
  }
  </code></pre>
  
  <p>If I use <code>getChildFragmentManager</code> instead of <code>getFragmentManager</code> as mentioned in the second answer I get</p>
  
  <pre><code>06-25 06:55:32.045: E/AndroidRuntime(2797): java.lang.IllegalArgumentException: No view found for id 0x7f06003c (com.amberroad.sigmaticket:id/pager) for fragment SearchResultFragment{b2fed358 #0 id=0x7f06003c}
  </code></pre>
  
  <p>Sorry for the lengthy question, how should I solve this?</p>