So I am trying to save my SeekBar values onPause and then have them reappear onResume. My code works but for some reason only the first in the list is being saved. If I change reds SeekBar then that saves but nothing else, if I leave red at 0 and move green to 40 then that green will save but then if I change red, only red will save. I do not know why this is happening. I have to use onResume or onPause or else I would make my variables static.
import android.content.SharedPreferences;
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.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.graphics.Color;
public class ColorChooserActivity extends ActionBarActivity
implements OnSeekBarChangeListener {
// define variables for the widgets
private SeekBar redSeekBar;
private SeekBar greenSeekBar;
private SeekBar blueSeekBar;
private SeekBar alphaSeekBar;
private View colorView;
private TextView colorTextView;
//instance variable
private int colorValue = 0;
private int redValue = 0;
private int greenValue = 0;
private int blueValue = 0;
private int alphaValue = 0;
private String output;
// define the SharedPreferences object
private SharedPreferences savedValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_chooser);
// get references to the widgets
redSeekBar = (SeekBar) findViewById(R.id.redSeekBar);
greenSeekBar = (SeekBar) findViewById(R.id.greenSeekBar);
blueSeekBar = (SeekBar) findViewById(R.id.blueSeekBar);
alphaSeekBar = (SeekBar) findViewById(R.id.alphaSeekBar);
colorView = (View) findViewById(R.id.colorView);
colorTextView = (TextView) findViewById(R.id.colorTextView);
// add listeners
redSeekBar.setOnSeekBarChangeListener(this);
greenSeekBar.setOnSeekBarChangeListener(this);
blueSeekBar.setOnSeekBarChangeListener(this);
alphaSeekBar.setOnSeekBarChangeListener(this);
// get SharedPreferences object
savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_color_chooser, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause() {
// save the instance variables
SharedPreferences.Editor editor = savedValues.edit();
editor.putInt("redValue", redValue);
editor.putInt("greenValue", greenValue);
editor.putInt("blueValue", blueValue);
editor.putInt("alphaValue", alphaValue);
editor.putInt("colorValue", colorValue);
editor.putString("output", output);
editor.commit();
super.onPause();
}
@Override
public void onResume() {
//get the instance variables
redValue = savedValues.getInt("redValue", redValue);
greenValue = savedValues.getInt("greenValue", greenValue);
blueValue = savedValues.getInt("blueValue", blueValue);
alphaValue = savedValues.getInt("alphaValue", alphaValue);
colorValue = savedValues.getInt("colorValue", colorValue);
output = savedValues.getString("output", output);
// set widgets
colorView.setBackgroundColor(colorValue);
colorTextView.setText(output);
redSeekBar.setProgress(redValue);
greenSeekBar.setProgress(greenValue);
blueSeekBar.setProgress(blueValue);
alphaSeekBar.setProgress(alphaValue);
super.onResume();
}
// argb method
public int argb(int a, int r, int g, int b){
return Color.argb(a, r, g, b);
}
//*****************************************************
// Event handler for the SeekBar
//*****************************************************
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
redValue = redSeekBar.getProgress();
greenValue = greenSeekBar.getProgress();
blueValue = blueSeekBar.getProgress();
alphaValue = alphaSeekBar.getProgress();
colorValue = argb(alphaValue, redValue, greenValue, blueValue);
colorView.setBackgroundColor(colorValue);
output = "Red: " + redValue + " Green: " + greenValue +
" Blue: " + blueValue + " Alpha: " + alphaValue;
colorTextView.setText(output);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar){
}
}
I think the problem is in your onProgressChanged
method. You have several seekbars, I suggest you implement a seperate OnSeekBarChangeListener
for each one of them or check for the ids of the seekbar before saving its values.
Basically, you should have something like this in your onCreate
method (based on an SO user's comment to your question):
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch (seekBar.getId()) {
case R.id.redSeekBar:
redValue = redSeekBar.getProgress();
break;
case R.id.greenSeekBar:
greenValue = greenSeekBar.getProgress();
break;
case R.id.blueSeekBar:
blueValue = blueSeekBar.getProgress();
break;
case R.id.alphaSeekBar:
alphaValue = alphaSeekBar.getProgress();
break;
}
colorValue = argb(alphaValue, redValue, greenValue, blueValue);
colorView.setBackgroundColor(colorValue);
output = "Red: " + redValue + " Green: " + greenValue + " Blue: "
+ blueValue + " Alpha: " + alphaValue;
colorTextView.setText(output);
}
I hope this helps.