0

나는 다음과 같은 코드를 가지고 : 나는 "passPhraseExample"에 값을 입력 할 때onPause()의 SharedPreferences에 저장된 값이 onResume()에 복원되지 않는 이유는 무엇입니까?

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를 다음 활동에 이동 한 다음 (에뮬레이터에서) 다시 이동합니다 passPhraseExample 글고 치기가 비어 있습니다. 입력 한 값을 저장하고 저장 한 다음 복원해야합니까?

답변

3

변경 내용을 커밋하지 마십시오.
onPause()에 문자열을 쓰고 editor.commit()으로 전화하십시오. 그렇지 않으면 변경 사항이 저장되지 않습니다.