title: Save/Restore fragments state android
body: <p>I am trying to create an app that uses Jack Wharton's ViewPager library (<a href="https://github.com/JakeWharton/Android-ViewPagerIndicator">here</a>) using only images for every page (something like Prixing (<a href="https://play.google.com/store/apps/details?id=fr.epicdream.beamy&amp;feature=search_result#?t=W251bGwsMSwxLDEsImZyLmVwaWNkcmVhbS5iZWFteSJd">here</a>) main screen).
  Everything works ok, except for the saveInstance in the Fragment. </p>
  
  <p>In Jack Wharton's example, he stores the text in a String variable, named mContent, and restores it in onCreate, but in my case, what should I do? Save/Restore a bitmap?! Any objective answer would be much appreciated!</p>
  
  <p>I'm quite new to this Fragment using applications, cause every example I look at, presents only the basics, and it's getting difficult upon more complex ones.</p>
  
  <p>PS. If is it usefull to know, I'm using CirclePageIndicator.</p>
  
  <p>Current Fragment Code here: </p>
  
  <pre><code>    public final class SpecialOfferFragment extends Fragment {
  
      private int imageResourceId;
  
      public static SpecialOfferFragment newInstance(int i) {
  
          //probably I'll use a bitmap(downloaded) as parameter instead of using static images
          SpecialOfferFragment fragment = new SpecialOfferFragment();
  
          fragment.imageResourceId = i;
  
          return fragment;
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // if ((savedInstanceState != null) { }
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          ImageView image = new ImageView(getActivity());
          image.setImageResource(imageResourceId);
          image.setScaleType(ScaleType.FIT_XY);
  
          LinearLayout layout = new LinearLayout(getActivity());
          layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                  LayoutParams.FILL_PARENT));
  
          layout.setGravity(Gravity.CENTER);
          layout.addView(image);
  
          return layout;
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          //smth to save here..
      }
  }
  </code></pre>
  
  <p>In this current state of the app, I get the following Exception: </p>
  
  <pre><code>04-12 07:28:17.760: E/AndroidRuntime(31903): FATAL EXCEPTION: main 
  04-12 07:28:17.760: E/AndroidRuntime(31903): java.lang.NullPointerException
  04-12 07:28:17.760: E/AndroidRuntime(31903): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.Activity.performSaveInstanceState(Activity.java:1113)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2804)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2862)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.ActivityThread.access$900(ActivityThread.java:127)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1175)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.os.Handler.dispatchMessage(Handler.java:99)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.os.Looper.loop(Looper.java:137)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at android.app.ActivityThread.main(ActivityThread.java:4511)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at java.lang.reflect.Method.invoke(Method.java:511)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
  04-12 07:28:17.760: E/AndroidRuntime(31903):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>This happens whenever I pause/stop the activity containing this fragment, like pushing the Home button.</p>
  
  <p>**</p>
  
  <h2>EDIT with code:</h2>
  
  <p>**</p>
  
  <pre><code> public class MainMenu extends FragmentActivity {
  
      //private List&lt;CategoriesHolder&gt; categoriesList = new ArrayList&lt;CategoriesHolder&gt;();
      //private CategoriesAdapter categoriesAdapter = null;
      //private GridView gv_mainmenu_categories;
  
      SpecialOfferFragmentAdapter mAdapter;
      ViewPager mPager;
      PageIndicator mIndicator;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_mainmenu);
  
          linkUI();
          setAction();
  
          // just for testing
          String[] imagesUrls = null;
  
          mAdapter = new SpecialOfferFragmentAdapter(getSupportFragmentManager(),
                  this, imagesUrls);
  
          //categoriesAdapter = new CategoriesAdapter(this, categoriesList);
  
          // used for Categories GridView
          //gv_mainmenu_categories.setAdapter(categoriesAdapter);
  
          // used for ViewPager
          mPager.setAdapter(mAdapter);
          mIndicator.setViewPager(mPager);
  
      }
  
      private void linkUI() {
          mPager = (ViewPager) findViewById(R.id.vp_mainmenu_special_offers);
          mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
  
          //gv_mainmenu_categories = (GridView) findViewById(R.id.gv_mainmenu_categories);
  
      }
  
      private void setAction() {
  
      }
  } 
  
      public class SpecialOfferFragmentAdapter extends FragmentPagerAdapter implements
          IconPagerAdapter {
  
      private int[] mCarouselImages = new int[] { R.drawable.pic1,
              R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5
  
      };
  
      private String[] imagesUrls;
      private Context context;
  
      public static final int[] ICONS = new int[] {
              R.drawable.perm_group_calendar, R.drawable.perm_group_camera,
              R.drawable.perm_group_device_alarms, R.drawable.perm_group_location };
  
      private int mCount = mCarouselImages.length;
  
      public SpecialOfferFragmentAdapter(FragmentManager fm, Context context,
              String[] imagesUrls) {
          super(fm);
          this.context = context;
          this.imagesUrls = imagesUrls;
      }
  
      @Override
      public Fragment getItem(int position) {
  
          return SpecialOfferFragment.newInstance(mCarouselImages[position]);
      }
  
      @Override
      public int getCount() {
          return mCount;
      }
  
      @Override
      public int getIconResId(int index) {
          return ICONS[index % ICONS.length];
      }
  
      public void setCount(int count) {
          if (count &gt; 0 &amp;&amp; count &lt;= 10) {
              mCount = count;
              notifyDataSetChanged();
          }
      }
  }
  </code></pre>