title: setCurrentItem call on ViewPager causes app to crash with outofmemory error
body: <p>I am trying to create an app that has a bunch of low res images (each image around 50 - 70 KB). One could swipe through the image library, or based on certain buttons, go to specific image in the library. I used the ViewPager using the FragmentStatePagerAdapter (FSPA) as I understand that this is the most memory optimal way for swiping through images. I am expecting that this approach ensures that only 3 images/fragments are alive at any given point in time, hence there shouldn't be a memory issue as such. This approach works fine on most emulators. However, when I try with Nexus One emulator (I am using Android Studio), I see an issue. The swiping part works fine, but if I use a button to go to a specific page (using viewPager.SetCurrentItem() method), the app crashes with outofmemory error. What do I need to do to get this working (going to a particular image on click of a button)?</p>
  
  <p>I have a main activity page, and two fragment pages. Here is the code:</p>
  
  <p>Main activity page:</p>
  
  <pre><code>    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.example.tryslider.tryslider.MainActivity"&gt;
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>fragment pager (with the ViewPager and button):</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:gravity="center_horizontal"
      android:orientation="vertical"
      android:background="#5b9bd5"
      android:padding="4dip" &gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0"
          android:measureWithLargestChild="true"
          android:orientation="horizontal" &gt;
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/goButtonLabel"
              android:id="@id/goButton"
              android:textColor="#ffffff"
              android:background="@drawable/abc_ab_transparent_dark_holo" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
      &lt;/android.support.v4.view.ViewPager&gt;
  
          &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>fragment that contains the image:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="match_parent"&gt;
  
  
      &lt;ImageView
          android:id="@id/imageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/text"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="36dp"
          android:src="@drawable/first" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>This is my main activity code (notice the number in SetCurrentItem - I just move to an arbitrary image in the stack, say 5th image in this case (out of a total of 10 images):</p>
  
  <pre><code>package com.example.tryslider.tryslider;
  
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  
  import java.text.SimpleDateFormat;
  
  public class MainActivity extends ActionBarActivity {
  
      static final int ITEMS = 10;
      MyAdapter viewAdapter;
      ViewPager viewPager;
      private Button goButton;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_pager);
  
          viewAdapter = new MyAdapter(getSupportFragmentManager());
          viewPager = (ViewPager) findViewById(R.id.pager);
          viewPager.setAdapter(viewAdapter);
  
          goButton = (Button) findViewById(R.id.goButton);
  
          goButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  viewPager.setCurrentItem(5, false);
              }
          });
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public static class MyAdapter extends FragmentStatePagerAdapter {
          public MyAdapter(FragmentManager fragmentManager) {
              super(fragmentManager);
          }
  
          @Override
          public int getCount() {
              return ITEMS;
          }
  
          @Override
          public Fragment getItem(int position) {
              return ImageFragment.init(position);
          }
  
          @Override
          public void destroyItem(View collection, int position, Object o) {
              super.destroyItem(collection, position, o);
              View view = (View)o;
              ((ViewPager) collection).removeView(view);
              view = null;
          }
  
      }
  
  }
  </code></pre>
  
  <p>And the image fragment code:</p>
  
  <pre><code>package com.example.tryslider.tryslider;
  
  /**
   * Created by rahul on 8/3/14.
   */
  
  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.AdapterView;
  import android.widget.ImageView;
  
  
  public class ImageFragment extends Fragment {
      private int index;
      private int[] mImages = new int[] {
              R.drawable.first,
              R.drawable.second,
              R.drawable.third,
              R.drawable.fourth,
              R.drawable.fifth,
              R.drawable.sixth,
              R.drawable.seventh,
              R.drawable.eighth,
              R.drawable.ninth,
              R.drawable.tenth
      };
      static ImageFragment init(int val) {
          ImageFragment myFrag = new ImageFragment();
          Bundle args = new Bundle();
          args.putInt("val", val);
          myFrag.setArguments(args);
          return myFrag;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          index = getArguments() != null ? getArguments().getInt("val") : 1;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View layoutView = inflater.inflate(R.layout.fragment_image, container,
                  false);
  
          ImageView imageView = (ImageView) layoutView.findViewById(R.id.imageView);
          imageView.setImageResource(mImages[index]);
  
          return layoutView;
      }
  }
  </code></pre>
  
  <p>I tried going through many suggestions on SO and otherwise, but am struggling with this at the moment. Any help is greatly appreciated.</p>