-1

I am creating a very simple application that only has a seek bar that the user can use. My code looks like this :

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int number=50;
    SeekBar sb = (SeekBar)findViewById(R.id.slider);
    sb.setMax(100);
    sb.setProgress(number);
    sb.setOnSeekBarChangeListener(this);
}

@Override
public void onProgressChanged(SeekBar v, int progress, boolean isUser) {
    TextView tv = (TextView)findViewById(R.id.percent);
    tv.setText(Integer.toString(progress)+"%");


}

What i want to do is store the seek bars state and when the application is killed and the user opens it again , the last change is displayed on the seek bar and not the default. So i must use shared preferences on my int number variable? I cant understand how i can do that since when the application starts again the line int number=50 will be run again , so the value will go to 50 again.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
donparalias
  • 1,834
  • 16
  • 37
  • 60

4 Answers4

1

StackOverflow is not your research assistant. The task you are trying to achieve is ridiculously simple.

Consult the documentation for more information.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

This can be modified for whatever datatype you need:

private void loadSettings(){
    if(prefs == null) prefs = mContext.getSharedPreferences(PREF_KEY, 0);

    yourString = prefs.getString(YOUR_KEY, ""); 
}
private void saveSettings(){
    if(prefs == null) prefs = mContext.getSharedPreferences(PREF_KEY, 0);

    SharedPreferences.Editor prefsEd = prefs.edit();

    prefsEd.putString(YOUR_KEY, yourString);

    prefsEd.commit();
}
Cruceo
  • 6,763
  • 2
  • 31
  • 52
0

You can do this with both a sharedPreference or the savedInstanceState.

Check out Saving Android Activity state using Save Instance State

Of course the value to always doing to be 10 in that hardcoded assignment. You'll need to pull that value our of the sharedpref if you decide to go that route.

Community
  • 1
  • 1
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
0

something like this ought to work:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SharedPreferences sp;
    SharedPreferences.Editor spe;


    sp = PreferenceManager.getDefaultSharedPreferences(this);
    spe = sp.edit();


    int number= sp.getInt("seekNum", 50);
    SeekBar sb = (SeekBar)findViewById(R.id.slider);
    sb.setMax(100);
    sb.setProgress(number);
    sb.setOnSeekBarChangeListener(this);
}

@Override
public void onProgressChanged(SeekBar v, int progress, boolean isUser) {
    TextView tv = (TextView)findViewById(R.id.percent);
    tv.setText(Integer.toString(progress)+"%");


}


@Override
public void onStop(){
    super.onStop();

    spe.putInt("seekNum", sb.getProgress());
    spe.commit();
}
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Well i put outside the functions : SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor spe = sp.edit(); SeekBar sb = (SeekBar)findViewById(R.id.slider); so that all my functions can see that but it crashes on run – donparalias Jun 07 '12 at 20:28