Search code examples
androidsharedpreferencesandroid-sharedpreferences

SharedPreferences not persisting through Android app


I have an IntroActivity where I'm saving a value that's saved from an EditText, in a shared pref.

In my SettingsActivity I try to edit that value, but even when I call the shared preferences value with the key I just edited, it still shows the old value. What am I doing wrong?

Here is the IntroActivity

public class IntroActivity extends Activity {

    String PREF = "MyPrefs";
    Button mDone;
    EditText mTemperature;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);

        mDone = (Button) findViewById(R.id.done);
        mTemperature = (EditText) findViewById(R.id.temperature);

        mDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int temperature = Integer.parseInt(mTemperature.getText().toString());
                SharedPreferences preferences = getSharedPreferences(PREF, Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = preferences.edit();
                edit.putInt("sweater", temperature);
                edit.commit();

                Intent intent = new Intent(IntroActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

Here is the SettingsActivity

public class SettingsActivity extends ActionBarActivity {

    Button mDone;
    EditText mTemperature;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mDone = (Button) findViewById(R.id.done);
        mTemperature = (EditText) findViewById(R.id.temperature);

        mDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences preferences = getSharedPreferences("sweater", 0);
                SharedPreferences.Editor edit = preferences.edit();

                int temp = preferences.getInt("sweater", 0);
                int updateSweater = Integer.parseInt(mTemperature.getText().toString());
                edit.remove("sweater");
                edit.putInt("sweater", updateSweater);
                boolean saved = edit.commit();
                preferences.getInt("sweater", 0);

                Toast.makeText(SettingsActivity.this, "Sweater Weather Updated", Toast.LENGTH_SHORT).show();

                Map<String, ?> map = preferences.getAll();
                for(Map.Entry<String,?> entry : map.entrySet()){
                    Log.d("map values",entry.getKey() + ": " +
                            entry.getValue().toString());
                }
                Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

Solution

  • I try to edit that value, but even when I call the shared preferences value with the key I just edited, it still shows the old value. What am I doing wrong?

    Because SharedPreferences name is different in both Activities.

    In IntroActivity using MyPrefs and in SettingsActivity using sweater.

    Use same name in both Activities. to get it work change sweater to MyPrefs in SettingsActivity:

    SharedPreferences preferences = getSharedPreferences("MyPrefs", 0);