为什么我的值保存到onPause()中的SharedPreferences没有在onResume()中恢复?

时间:2011-12-08 03:16:36

标签: android android-emulator android-widget android-preferences

我有以下代码:

    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为空。它不应该具有我输入的值,保存,然后“恢复”吗?

1 个答案:

答案 0 :(得分:3)

您不会提交更改 在editor.commit()中写入字符串后调用onPause()。否则,您的更改不会保存。