Rafa
Rafa

Reputation: 3349

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);
            }
        });
    }
}

Upvotes: 0

Views: 66

Answers (3)

Vijay Rajput
Vijay Rajput

Reputation: 1091

These two line are different in both activities

SharedPreferences preferences = getSharedPreferences(PREF, Context.MODE_PRIVATE);

and

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

For this problem you have to create two pref files and read the values from another file.

Upvotes: 2

Ashwin Valento
Ashwin Valento

Reputation: 1008

I believe in your SettingsActivity, you need to use SharedPreferences preferences = getSharedPreferences("MyPrefs", 0);

Instead of SharedPreferences preferences = getSharedPreferences("sweater", 0);

You are storing the value in "sweater" Preference and loading it from "MyPrefs".

Upvotes: 2

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

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);

Upvotes: 3

Related Questions