title: How do I make my ArrayList<ColorSaver> persistent in android?
body: <p>I have the <code>ArrayList</code>...</p>
  
  <pre><code> ArrayList&lt;ColorSaver&gt; tempList = new ArrayList&lt;ColorSaver&gt;();
  </code></pre>
  
  <p>and I want it so that when the user closes the app or leaves the app, all the <code>ColorSaver</code> objects in the <code>ArrayList</code> will be there when the user reopens the app. I would prefer to use the <code>SharedPreferences</code> but I can't do that because the list is a custom object...
  I have looked around and found out that I can do a serializable but I tried that and failed horribly, so if somebody could guide me through the serializable deal that would be great. Oh and where do I put the code, like in <code>onCreate()</code> in my <code>mainActivity</code> or in the <code>activity</code> that is displaying the <code>ArrayList</code></p>
  
  <p>My mainActivity class</p>
  
  <pre><code>    public class MainActivity extends Activity {
  
      ArrayList&lt;ColorSaver&gt; tempList = new ArrayList&lt;ColorSaver&gt;();
      private static final String TAG = "Main Activity";
      public static final String PREFS_NAME = "MyPrefsFile";
      final Intent intent = new Intent();
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      {
          final NumberPicker rednp = (NumberPicker) findViewById(R.id.redNumberPicker1);
          final NumberPicker bluenp = (NumberPicker) findViewById(R.id.blueNumberPicker);
          final NumberPicker greennp = (NumberPicker) findViewById(R.id.greenNumberPicker);
          switch(item.getItemId())
          {
              case R.id.save:
                  Log.i(TAG, "Save item clicked!");
                  Intent intent = new Intent(this, SaveActivity.class);
                  intent.putExtra("RedValue", rednp.getValue());
                  intent.putExtra("BlueValue", bluenp.getValue());
                  intent.putExtra("GreenValue", greennp.getValue());
                  intent.putExtra("temparray", tempList);
                  startActivity(intent);
              return true;
  
              case R.id.recall:
                  Log.i(TAG, "Recall item clicked!");
                  Intent intent2 = new Intent(this, RecallActivity.class);
                  intent2.putExtra("temparray", tempList);
                  startActivity(intent2);
              return true;
  
              default:
                  return super.onOptionsItemSelected(item);
          }//End Switch
      }
  
      @SuppressWarnings("unchecked")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //ArrayList&lt;ColorSaver&gt; tempList = new ArrayList&lt;ColorSaver&gt;();
  
          Bundle extras = getIntent().getExtras();
  
          final SurfaceView sView = (SurfaceView) findViewById(R.id.surfaceView1);
  
  
          final NumberPicker np = (NumberPicker) findViewById(R.id.redNumberPicker1);
          np.setMaxValue(255);
          np.setMinValue(0);
  
          final NumberPicker np2 = (NumberPicker) findViewById(R.id.greenNumberPicker);
          np2.setMaxValue(255);
          np2.setMinValue(0);
  
          final NumberPicker np3 = (NumberPicker) findViewById(R.id.blueNumberPicker);
          np3.setMaxValue(255);
          np3.setMinValue(0);
  
  
          if( extras != null )
          {
              np.setValue(extras.getInt("savedRValue"));
              //np.setValue(intent.getIntExtra("savedRValue", 255));
              np2.setValue(extras.getInt("savedGValue"));
              //np2.setValue(intent.getIntExtra("savedGValue", 255));
              np3.setValue(extras.getInt("savedBValue"));
              //np3.setValue(intent.getIntExtra("savedBValue", 255));
              tempList = (ArrayList&lt;ColorSaver&gt;) extras.getSerializable("array");
              sView.setBackgroundColor(Color.argb(255, np.getValue(), np2.getValue(), np3.getValue()));
          }
          else
          {
              Log.i(TAG, "I just don't get it...WTF");
          }
  
  
          np.setOnValueChangedListener( new NumberPicker.
                  OnValueChangeListener() {
                  @Override
                  public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
                 {
                      int rednum, greennum, bluenum;
                      rednum = np.getValue();
                      greennum = np2.getValue();
                      bluenum = np3.getValue();
  
  
                      sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum));
  
                  }
              });
  
          //GREEN NUMBERPICKER LISTENER
          np2.setOnValueChangedListener( new NumberPicker.
                  OnValueChangeListener() {
                  @Override
                  public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
                 {
                      int rednum, greennum, bluenum;
                      rednum = np.getValue();
                      greennum = np2.getValue();
                      bluenum = np3.getValue();
  
  
                      sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum));
  
                  }
              });
  
          np3.setOnValueChangedListener( new NumberPicker.
                  OnValueChangeListener() {
                  @Override
                  public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
                 {
                      int rednum, greennum, bluenum;
                      rednum = np.getValue();
                      greennum = np2.getValue();
                      bluenum = np3.getValue();
  
  
  
  
                      sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum));
  
                  }
              });
  
  
      }//End onCreate()
  
      @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;
      }//END onCreateOptionsMenu()
  
  
  }//END CLASS
  </code></pre>
  
  <p>My <code>saveActivity</code>, where the user saves their color combo to the <code>ArrayList</code>...</p>
  
  <pre><code>    public class SaveActivity extends Activity implements Serializable {
  
      private static final String TAG = "Save Activity";
      public ArrayList&lt;ColorSaver&gt; savedColors = new ArrayList&lt;ColorSaver&gt;();
  
      @SuppressWarnings("unchecked")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_save);
          // Show the Up button in the action bar.
          setupActionBar();
          Bundle extras = getIntent().getExtras();
  
          final Intent intent1 = new Intent(this, MainActivity.class);
          Button saveButton = (Button) findViewById(R.id.saveButton1);
          final EditText nameField = (EditText) findViewById(R.id.colorNameField);
          //final Intent intent = new Intent();
  
          savedColors = (ArrayList&lt;ColorSaver&gt;) extras.getSerializable("temparray");
  
          //Making sure the savedColors arrayList has something in it.
          if( savedColors.isEmpty() )
          {
              ColorSaver temp = new ColorSaver("Rockies Purple", 180, 80, 255);
              savedColors.add(temp);
          }
  
  
  
  
  
          saveButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  // Perform action on click
                  Bundle extras = getIntent().getExtras();
  
                  int redcolor, greencolor, bluecolor;
                  redcolor = extras.getInt("RedValue");
                  greencolor = extras.getInt("GreenValue");
                  bluecolor = extras.getInt("BlueValue");
                  String colorName = nameField.getText().toString();
  
                  //Build the new color and add it to the arrayList
                  ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor);
                  savedColors.add(saver);
                  intent1.putExtra("array", savedColors);
  
                  Log.i(TAG, savedColors.get(savedColors.size()-1).getColorName());
                  startActivity(intent1);
  
              }
          });
  
      }//END OnCreate()
  
      /**
       * Set up the {@link android.app.ActionBar}.
       */
      private void setupActionBar() {
  
          getActionBar().setDisplayHomeAsUpEnabled(true);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.save, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case android.R.id.home:
              // This ID represents the Home or Up button. In the case of this
              // activity, the Up button is shown. Use NavUtils to allow users
              // to navigate up one level in the application structure. For
              // more details, see the Navigation pattern on Android Design:
              //
              // http://developer.android.com/design/patterns/navigation.html#up-vs-back
              //
              NavUtils.navigateUpFromSameTask(this);
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  }//END CLASS
  </code></pre>
  
  <p>My <code>recallActivity</code> where the user recalls their color combos...</p>
  
  <pre><code>  public class RecallActivity extends SaveActivity {
  
      private static final String TAG = "Recall Activity";
      ArrayList&lt;ColorSaver&gt; colorsArray = new ArrayList&lt;ColorSaver&gt;();
      SaveActivity sActivity = new SaveActivity();
  
  
      @SuppressWarnings("unchecked")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_recall);
          // Show the Up button in the action bar.
          setupActionBar();
  
          final Intent intent1 = new Intent(this, MainActivity.class);
          final Spinner colorList = (Spinner) findViewById(R.id.colorsSpinner);
          Button grabButton = (Button) findViewById(R.id.grabButton);
          Bundle extras = getIntent().getExtras();
  
          colorsArray = (ArrayList&lt;ColorSaver&gt;) extras.getSerializable("temparray");
  
          //Load the spinner with the saved colors
          addColorNames(colorsArray);
  
          grabButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  // Perform action on click
  
                  ColorSaver selectedItem = (ColorSaver) colorList.getSelectedItem();
  
                  int redValue, greenValue, blueValue;
                  String name;
                  redValue = selectedItem.getRedValue();
                  greenValue = selectedItem.getGreenValue();
                  blueValue = selectedItem.getBlueValue();
                  name = selectedItem.getColorName();
                  intent1.putExtra("savedRValue", redValue);
                  intent1.putExtra("savedGValue", greenValue);
                  intent1.putExtra("savedBValue", blueValue);
                  intent1.putExtra("savedName", name);
                  intent1.putExtra("array", colorsArray);
                  startActivity(intent1);
  
  
              }//END onClick
          });
      }
  
      /**
       * Set up the {@link android.app.ActionBar}.
       */
      private void setupActionBar() {
  
          getActionBar().setDisplayHomeAsUpEnabled(true);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.recall, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case android.R.id.home:
              // This ID represents the Home or Up button. In the case of this
              // activity, the Up button is shown. Use NavUtils to allow users
              // to navigate up one level in the application structure. For
              // more details, see the Navigation pattern on Android Design:
              //
              // http://developer.android.com/design/patterns/navigation.html#up-vs-back
              //
              NavUtils.navigateUpFromSameTask(this);
              return true;
          }
          return super.onOptionsItemSelected(item);
      }// END onOptionsItemSelected(MenuItem item)
  
  
      public void addColorNames(ArrayList&lt;ColorSaver&gt; colorsArray1)
      {
          colorsArray = colorsArray1;
          //if( !colorsArray.isEmpty() )
          //{
              Spinner colorsSpinner = (Spinner) findViewById(R.id.colorsSpinner);
  
              ArrayAdapter&lt;ColorSaver&gt; dataAdapter 
                  = new ArrayAdapter&lt;ColorSaver&gt;
                  (RecallActivity.this, android.R.layout.simple_spinner_item, colorsArray);
  
              dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
              colorsSpinner.setAdapter(dataAdapter);
              Log.i(TAG, savedColors.get(savedColors.size() - 1).toString());
          //}
          //else
          //{
          //  Log.i(TAG, "colorsSpinner came out to be null....WTF???");
  
          //}
  
      }//End addColorNames()
  
  }//END CLASS
  </code></pre>
  
  <p>I am greatful of any help!</p>