title: Remove Fragment Page from ViewPager in Android
body: <p>I'm trying to dynamically add and remove Fragments from a ViewPager, adding works without any problems, but removing doesn't work as expected.</p>
  
  <p>Everytime I want to remove the current item, the last one gets removed.</p>
  
  <p>I also tried to use an FragmentStatePagerAdapter or return POSITION_NONE in the adapter's getItemPosition method.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here's a basic example:</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements TextProvider {
  
      private Button mAdd;
      private Button mRemove;
      private ViewPager mPager;
  
      private MyPagerAdapter mAdapter;
  
      private ArrayList&lt;String&gt; mEntries = new ArrayList&lt;String&gt;();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          mEntries.add("pos 1");
          mEntries.add("pos 2");
          mEntries.add("pos 3");
          mEntries.add("pos 4");
          mEntries.add("pos 5");
  
          mAdd = (Button) findViewById(R.id.add);
          mRemove = (Button) findViewById(R.id.remove);
          mPager = (ViewPager) findViewById(R.id.pager);
  
          mAdd.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View view) {
                  addNewItem();
              }
          });
  
          mRemove.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View view) {
                  removeCurrentItem();
              }
          });
  
          mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this);
  
          mPager.setAdapter(mAdapter);
  
      }
  
      private void addNewItem() {
          mEntries.add("new item");
          mAdapter.notifyDataSetChanged();
      }
  
      private void removeCurrentItem() {
          int position = mPager.getCurrentItem();
          mEntries.remove(position);
          mAdapter.notifyDataSetChanged();
      }
  
      @Override
      public String getTextForPosition(int position) {
          return mEntries.get(position);
      }
      @Override
      public int getCount() {
          return mEntries.size();
      }
  
  
      private class MyPagerAdapter extends FragmentPagerAdapter {
  
          private TextProvider mProvider;
  
          public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
              super(fm);
              this.mProvider = provider;
          }
  
          @Override
          public Fragment getItem(int position) {
              return MyFragment.newInstance(mProvider.getTextForPosition(position));
          }
  
          @Override
          public int getCount() {
              return mProvider.getCount();
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>TextProvider.java</strong></p>
  
  <pre><code>public interface TextProvider {
      public String getTextForPosition(int position);
      public int getCount();
  }
  </code></pre>
  
  <p><strong>MyFragment.java</strong></p>
  
  <pre><code>public class MyFragment extends Fragment {
  
      private String mText;
  
      public static MyFragment newInstance(String text) {
          MyFragment f = new MyFragment(text);
          return f;
      }
  
      public MyFragment() {
      }
  
      public MyFragment(String text) {
          this.mText = text;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View root = inflater.inflate(R.layout.fragment, container, false);
  
          ((TextView) root.findViewById(R.id.position)).setText(mText);
  
          return root;
      }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;Button
          android:id="@+id/add"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="add new item" /&gt;
  
      &lt;Button
          android:id="@+id/remove"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="remove current item" /&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="0dip"
          android:layout_weight="1" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>fragment.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/position"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"
          android:textSize="35sp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>