Java中的持久性

时间:2013-04-01 23:48:34

标签: java android persistent

如何在Java中使用持久性?

1 个答案:

答案 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
    }
}