title: Fragment does not get displayed in viewpager
body: <p>PFB my code and my requirement and my research</p>
  
  <p>Requirement :</p>
  
  <p>I have 4 tabs (Tabs A,B,C,D) having 4 different Fragments (Fragments A,B,C,D on each corresponding tabs) and they are attached with viewpager </p>
  
  <p>I can swipe through tabs using the help of FragmentStatePagerAdapter where it saves the state of fragments to be swiped </p>
  
  <p>Now I have a list in Fragment say Fragment A and it has a button on whose click I need to call say new Fragment A1 . </p>
  
  <p>I have reffered the stack site Link : <a href="https://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager">Replace Fragment inside a ViewPager</a></p>
  
  <p>But the FragmentA on TabA gets disappears and does not display FragmentA1 on Tab A what is the issue . Kindly help </p>
  
  <p>My PageAdapter .java class </p>
  
  <pre><code>import java.util.List;
  
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.app.FragmentStatePagerAdapter;
  
  /**
  * The &lt;code&gt;PagerAdapter&lt;/code&gt; serves the fragments when paging.
  * @author mwho
  */
  public class PagerAdapter extends FragmentStatePagerAdapter {
  
     private List&lt;Fragment&gt; fragments;
     private final FragmentManager mFragmentManager = null;
     private Fragment mFragmentAtPos0;
  
     /**
      * @param fm
      * @param fragments
      */
     public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
         super(fm);
         this.fragments = fragments;
     }
     /* (non-Javadoc)
      * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
      */
     @Override
     public Fragment getItem(int position) {
  
  
         if (position == 0)
         {
             if (mFragmentAtPos0 == null)
             {
                 mFragmentAtPos0 = FarmerInformationFragment.newInstance(new FirstPageFragmentListener()
                 {
                     public void onSwitchToNextFragment()
                     {
                         mFragmentManager.beginTransaction().remove(mFragmentAtPos0).commit();
                         mFragmentAtPos0 = FieldVisitFragment.newInstance();
                         notifyDataSetChanged();
                     }
                 });
             }
             return mFragmentAtPos0;
         }
         else
             return FieldVisitFragment.newInstance();
  
  
  
  
      //   return this.fragments.get(position);
     }
  
     /* (non-Javadoc)
      * @see android.support.v4.view.PagerAdapter#getCount()
      */
     @Override
     public int getCount() {
         return this.fragments.size();
     }
  
  
     @Override
     public int getItemPosition(Object object)
     {
         if (object instanceof FarmerInformationFragment &amp;&amp; mFragmentAtPos0 instanceof FieldVisitFragment)
             return POSITION_NONE;
         return POSITION_UNCHANGED;
     }
  
  
  
  }
  </code></pre>
  
  <p>Interface </p>
  
  <pre><code>public interface FirstPageFragmentListener {
      void onSwitchToNextFragment();
  }
  </code></pre>
  
  <p>FragmentA1 </p>
  
  <pre><code>import com.example.fms.R;
  
  import android.annotation.SuppressLint;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.LinearLayout;
  
  @SuppressLint("NewApi")
  public class FieldVisitFragment extends Fragment {
  
  
  
      public static FieldVisitFragment newInstance() {
          FieldVisitFragment f = new FieldVisitFragment();
          return f;
      }
  
      /** (non-Javadoc)
       * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
       */
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          if (container == null) {
              // We have different layouts, and in one of them this
              // fragment's containing frame doesn't exist.  The fragment
              // may still be created from its saved state, but there is
              // no reason to try to create its view hierarchy because it
              // won't be displayed.  Note this is not needed -- we could
              // just run the code below, where we would create and return
              // the view hierarchy; it would just never be used.
              return null;
          }
          return (LinearLayout)inflater.inflate(R.layout.yield_estimation_screen2_layout, container, false);
      }
  
  }
  </code></pre>
  
  <p>Fragment A </p>
  
  <pre><code>import com.example.fms.R;
  
  import android.annotation.SuppressLint;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.RelativeLayout;
  import android.widget.Toast;
  
  @SuppressLint("NewApi")
  public class FarmerInformationFragment extends Fragment {
  
      ListView mLandHoldingList , mFamilyDetailsList , mBankDetailsList , mOtherDetailsList;
      ImageView mArrowLandHolding  , mArrowFamilyHolding , mArrowBankHolding , mArrowOtherHolding ;
      RelativeLayout mLandHoldingRelativeLayout , mFamilyDetailsRelativeLayout , mBankDetailsRelativeLayout , mOtherDetailsRelativeLayout ;
      static boolean mLandFlg =false , mFamilyFlg = false , mBankFlg = false , mOtherDetailsFlg = false ;
      BroadT mScreenTimeOutReceiver;
      BroadcastReceiver mBroadcastReceiver;
  
      static FirstPageFragmentListener firstPageFragmentListenerFrag;
  
  
  
      public static FarmerInformationFragment newInstance(FirstPageFragmentListener firstPageFragmentListener) {
          FarmerInformationFragment f = new FarmerInformationFragment();
          firstPageFragmentListenerFrag = firstPageFragmentListener;
          return f;
      }
  
  
      /** (non-Javadoc)
       * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
       */
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          if (container == null) {
              // We have different layouts, and in one of them this
              // fragment's containing frame doesn't exist.  The fragment
              // may still be created from its saved state, but there is
              // no reason to try to create its view hierarchy because it
              // won't be displayed.  Note this is not needed -- we could
              // just run the code below, where we would create and return
              // the view hierarchy; it would just never be used.
              return null;
          }
  
  
          View view = inflater.inflate(R.layout.tab_frag_farmer_info_layout, container,false);
          mScreenTimeOutReceiver = new BroadT();
          mLandHoldingList = (ListView)view.findViewById(R.id.landHoldingList);
          mFamilyDetailsList = (ListView)view.findViewById(R.id.familyDetailsList);
          mBankDetailsList = (ListView)view.findViewById(R.id.bankDetailsList);
          mOtherDetailsList = (ListView)view.findViewById(R.id.otherDetailsList);
  
  
          mLandHoldingList.setAdapter(new LandHoldingListAdapter(getActivity(),null ,mScreenTimeOutReceiver,FarmerInformationFragment.this.getActivity()));
  
  
  
          mArrowLandHolding = (ImageView)view.findViewById(R.id.arrowLandHolding);
          mArrowFamilyHolding = (ImageView)view.findViewById(R.id.arrowFamily);
          mArrowBankHolding = (ImageView)view.findViewById(R.id.arrowBank);
          mArrowOtherHolding = (ImageView)view.findViewById(R.id.arrowOther);
  
  
          mLandHoldingRelativeLayout = (RelativeLayout)view.findViewById(R.id.landrel);
          mFamilyDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.familyrel);
          mBankDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.bankrel);
          mOtherDetailsRelativeLayout = (RelativeLayout)view.findViewById(R.id.otherrel);
  
  
  
          // Land 
  
          mLandHoldingRelativeLayout.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  if(mLandFlg)
                  {
                      mArrowLandHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                      mLandHoldingList.setVisibility(View.VISIBLE);
                      mLandFlg = false;
  
                  }
                  else
                  {
  
                      mArrowLandHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                      mLandHoldingList.setVisibility(View.GONE);
                      mLandFlg = true;
  
  
                  }
  
              }
          });
  
  
          // Family 
  
          mFamilyDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  if(mFamilyFlg)
                  {
                      mArrowFamilyHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                      mFamilyDetailsList.setVisibility(View.VISIBLE);
                      mFamilyFlg = false;
  
                  }
                  else
                  {
                      mArrowFamilyHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                      mFamilyDetailsList.setVisibility(View.GONE);
                      mFamilyFlg = true;
  
                  }
  
              }
          });
  
  
          // Bank 
  
          mBankDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  if(mBankFlg)
                  {
                      mArrowBankHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                      mBankDetailsList.setVisibility(View.VISIBLE);
                      mBankFlg = false;
  
                  }
                  else
                  {
  
                      mArrowBankHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                      mBankDetailsList.setVisibility(View.GONE);
                      mBankFlg = true;
  
  
                  }
  
              }
          });
  
  
          // Other 
  
          mOtherDetailsRelativeLayout.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  if(mOtherDetailsFlg)
                  {
                      mArrowOtherHolding.setImageDrawable(getResources().getDrawable(R.drawable.down_aarow));
                      mOtherDetailsList.setVisibility(View.VISIBLE);
                      mOtherDetailsFlg = false;
  
                  }
                  else
                  {
  
                      mArrowOtherHolding.setImageDrawable(getResources().getDrawable(R.drawable.up_aarow));
                      mOtherDetailsList.setVisibility(View.GONE);
                      mOtherDetailsFlg = true;
  
  
                  }
  
              }
          });
  
  
  
      /* mBroadcastReceiver = new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context context, Intent intent) {
                      Toast.makeText(context, "Recived", 
                                 Toast.LENGTH_LONG).show();
                      FieldVisitFragment fieldvisitFragment = new FieldVisitFragment();
                      FragmentManager fragmentManager = getFragmentManager();
                      FragmentTransaction fragmentTransaction = fragmentManager
                              .beginTransaction();
                      fragmentTransaction.replace(android.R.id.tabcontent, fieldvisitFragment);
                      fragmentTransaction.commit();
                  }
  
  
  
              };*/
  
  
  
  
  
  
  
  
  
  
  
  
  
          return view;
  
  
  
  
  
  
      }
  
  
  }
  </code></pre>
  
  <p>My BaseAdapter from List of Fragment A on Tab A which has button on whose click I call Fragment A1 </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  
  import com.example.fms.R;
  
  import android.annotation.SuppressLint;
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.IntentFilter;
  import android.os.Build;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebView.FindListener;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class LandHoldingListAdapter extends BaseAdapter{
  
      private LayoutInflater mInflater;
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; allElementDetails;
      private Context con;
      BroadcastReceiver mBroadcastReceiver;
      FragmentActivity fragmentActivity;
  
      public LandHoldingListAdapter (Context context,
              ArrayList&lt;HashMap&lt;String, String&gt;&gt; mapData , BroadcastReceiver mBroadcastReceiver, FragmentActivity fragmentActivity)
      {
          mInflater = LayoutInflater.from(context);
          allElementDetails = mapData;
          con = context;
          this.mBroadcastReceiver = mBroadcastReceiver;
          this.fragmentActivity=fragmentActivity;     
      }
  
      @Override
      public int getCount() {
          /*return allElementDetails.size();*/
  
          return 1 ;
      }
  
      @Override
      public Object getItem(int position) {
           //return allElementDetails.get(position);
           return 1;
      }
  
      @Override
      public long getItemId(int position) {
          return 1;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
  
  
          //HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
          //map = allElementDetails.get(position);
  
  
          convertView = mInflater.inflate(R.layout.farmer_info_land_holding_table_row, null);
  
          TextView rsNumberTxt = (TextView) convertView.findViewById(R.id.RSnumberTxt);
          TextView type = (TextView) convertView.findViewById(R.id.type);
          TextView year = (TextView) convertView.findViewById(R.id.year);
          ImageView visitImg = (ImageView)convertView.findViewById(R.id.visitButton);
          ImageView yieldImg = (ImageView)convertView.findViewById(R.id.yieldButton);
          ImageView landImg = (ImageView)convertView.findViewById(R.id.landButton);
  
          visitImg.setOnClickListener(new View.OnClickListener() {
  
              @SuppressWarnings("static-access")
              @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override
              public void onClick(View arg0) {/*
                  Toast.makeText(con, "Visit Clicked ", Toast.LENGTH_LONG).show();
                  con.registerReceiver(mBroadcastReceiver, new IntentFilter(
                          "start.fragment.action"));
              */
  
                  Toast.makeText(con, "clicked!", Toast.LENGTH_SHORT).show();
  
                  FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
  
                  ft.replace(R.id.viewpager, new FieldVisitFragment().newInstance());
                  ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                  ft.addToBackStack(null);
  
                  ft.commit();
  
              }
          });
  
          return convertView;
      }
  
  }
  </code></pre>