如何在Java中使用持久性?
答案 0 :(得分:1)
如果您将item.getItemId()
存储在您的SharedPreferences中的onOptionsItemSelected
内,然后将switch
块移动到新功能,以便在您的onCreate
中调用该新功能,该怎么办?恢复选择:
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = getSharedPreferences("prefName", 0);
int thicknessId = prefs.getInt("thicknessFieldName", 1); // Replace 1 with first-load state
switchScribbleView(thicknessId);
int colorId = prefs.getInt("colorFieldName", 4); // Replace 4 with first-load state
switchScribbleView(colorId);
//... rest of code
}
public boolean onOptionsItemSelected(MenuItem item) {
int menuId = item.getItemId();
//Get old values so they persist
SharedPreferences prefs = getSharedPreferences("prefName", 0);
int thicknessId = prefs.getInt("thicknessFieldName", 0);
int colorId = prefs.getInt("colorFieldName", 0);
if (menuId <= 2) // Control Thick and Thin
thicknessId = menuId;
else if (menuId >= 4) // Control Color
colorId = menuId;
Editor editor = getSharedPreferences("prefName", 0).edit();
editor.putInt("thicknessFieldName", thicknessId);
editor.putInt("colorFieldName", colorId);
editor.commit();
switchScribbleView(item.getItemId();
return true;
}
private void switchScribbleView(int menuId) {
switch (menuId) {
//... switch block here
}
}