I have the following code:
SharedPreferences KITPrefs;
EditText passPhraseExample;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
passPhraseExample = (EditText) findViewById(R.id.editTextPassPhraseExample);
KITPrefs = getPreferences(Activity.MODE_PRIVATE);
@Override
public void onResume() {
super.onResume();
passPhraseExample.setText(KITPrefs.getString("passPhraseExample", ""));
}
@Override
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = KITPrefs.edit();
editor.putString("passPhraseExample", passPhraseExample.getText()
.toString());
}
..yet when I enter a value in “passPhraseExample,” move to the next Activity, and then go back (in the Emulator), the passPhraseExample EditText is empty. Shouldn’t it have the value I entered into it, saved, and then “restored”?
You don’t commit your changes.
Call
editor.commit()after writing the string inonPause(). Otherwise your changes are not saved.