我有以下代码:
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());
}
..当我在“passPhraseExample”中输入值时,移动到下一个Activity,然后返回(在模拟器中),passPhraseExample EditText为空。它不应该具有我输入的值,保存,然后“恢复”吗?
答案 0 :(得分:3)
您不会提交更改
在editor.commit()
中写入字符串后调用onPause()
。否则,您的更改不会保存。