0

Right i know there's a lot of these types of questions on here but i'm yet to find one that works.

For the past 30 minutes or so i've playing with SharedPreferences and so far the values save with no problems at all but when you kill the process by either long pressing the back button or using a task manager and you go to restart the app, it'll go back to default values.

Now the default value is 50, as i'm doing my Equalizer and have a flat/reset button

So yeah, basically how do i save the values and keep them saved?!

Here's my various attempts:

Attempt 1:

     private int isFirstTime()
{
    SharedPreferences sp = getSharedPreferences("sliders", MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++) {
        editor.putInt("sliders",  sliders[i].getProgress());
        editor.putInt("bass_boost", bass_boost.getProgress());
        editor.commit();
    }
    return num_sliders;


}

Attempt 2:

  public int saveProgress(){
SharedPreferences sp = getSharedPreferences("sliders2", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("sliders",  sliders[0].getProgress());
editor.putInt("sliders2", sliders[1].getProgress());
editor.putInt("sliders3", sliders[2].getProgress());
editor.putInt("sliders4", sliders[3].getProgress());
editor.putInt("sliders5", sliders[4].getProgress());
editor.putInt("sliders6", sliders[5].getProgress());
editor.putInt("sliders7", sliders[6].getProgress());
editor.putInt("sliders8", sliders[7].getProgress());
editor.putInt("sliders9", sliders[8].getProgress());
editor.putInt("sliders10", sliders[9].getProgress());
editor.putInt("bass_boost", bass_boost.getProgress());
editor.commit();
return min_level - max_level;
}

Attempt 3:

  public int getProgress(){

 SharedPreferences sp = getSharedPreferences("sliders", MODE_PRIVATE);
 for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++) {
     num_sliders = sp.getInt("sliders", 0);
 }
 num_sliders = sp.getInt("bass_boost", 0);
 return num_sliders;
 }

Attempt 4:

   public int getProgress2(){

    SharedPreferences sp = getSharedPreferences("sliders",   Activity.MODE_PRIVATE);
     num_sliders = sp.getInt("sliders1", -1);
     num_sliders = sp.getInt("sliders2", -1);
     num_sliders = sp.getInt("sliders3", -1);
     num_sliders = sp.getInt("sliders4", -1);
     num_sliders = sp.getInt("sliders5", -1);
     num_sliders = sp.getInt("sliders6", -1);
     num_sliders = sp.getInt("sliders7", -1);
     num_sliders = sp.getInt("sliders8", -1);
     num_sliders = sp.getInt("sliders9", -1);
     num_sliders = sp.getInt("sliders10", -1);
     num_sliders = sp.getInt("bass_boost", -1);
     return num_sliders;
}

Any help will be hugely appreciated as i need to get the apps update rolled out!

Thank you.

  • `I need to get the apps update rolled out` as a side note: please don't rush to publish updates. –  Apr 28 '14 at 14:43

2 Answers2

1

I know this is an old question, but I just wasted two hours on this, might as well give my answer in case anyone encounters the same problem.

What I had to do to have settings properly survive across app kills / device reboots was:

  1. Use the editor to REMOVE the old value
  2. Commit
  3. Put the new value
  4. Commit

So, for example:

SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(Constants.PREF_STARTED_EXPERIMENTS);
        editor.commit();

        editor.putStringSet(Constants.PREF_STARTED_EXPERIMENTS, experiments);
        editor.commit();

Hope it saves someone some time.

Master_T
  • 7,232
  • 11
  • 72
  • 144
0

The problem could be null values being saved into your preferences file. Please try if this answer solves your problem.

Community
  • 1
  • 1