我尝试通过Android SharedPreference API在我的应用中保存用户菜单选项。尽管成功地提交了一个' 2'我的编辑器对象通过 editor.putInt(" woodType",woodIntCode)&发生onStop()时, editor.commit(),onCreate()期间settings.getInt(" woodType",0)返回的值始终为0.
为什么首选项文件没有为woodType返回值2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//Restore preferences
SharedPreferences settings = getPreferences(0); //MODE_PRIVATE = 0
prefWoodColor = settings.getInt("woodType", 0); //default color is 0 (dark wood) if no saved pref
}
//When app is stopped, update settings prefs so they persist into next launch
@Override
protected void onStop(){
super.onStop();
int woodKey = getGLSurfaceView().mRenderer.getWoodColor();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("woodType", woodKey);
// Commit the edits!
editor.commit();
}