SharedPreference不重新创建菜单

时间:2012-10-25 03:25:55

标签: android sharedpreferences

我正在尝试使用SharedPreferences存储首选项,然后根据SharedPreference更改菜单项的默认选中状态。但它似乎没有奏效。菜单选项一直保持到应用程序关闭为止。当我重新加载应用程序时,设置恢复为默认设置,而不是新的SharedPreference设置。

public class MainActivity extends Activity {

boolean prefs = true;
String FILENAME = "settings";
String string;

public static final String LOG_TAG = "dbryant423";
public static final String PREFS_NAME = "settings";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(LOG_TAG, "prefs value: " +prefs);

 }

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference == "us")
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference == "metric")
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true; 
}

// called when an item is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) { // decide which MenuItem was pressed based on it's id
    case R.id.menu_us:
            menuUS();

    case R.id.menu_metric:
            menuMetric();

    }
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (prefs == true)
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (prefs == false)
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true;
}

public void menuUS() {
    prefs = true;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "us");
    editor.commit();
}

public void menuMetric() {
    prefs = false;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "metric");
    editor.commit();
}

public void calculateCylinder(View v) {
    Intent calculateCylinderUS = new Intent(this, CalculateCylinder.class);
    Intent calculateCylinderMetric = new Intent(this, CalculateCylinderMetric.class);
     Log.v(LOG_TAG, "prefs value: " +prefs);
    if (prefs == true)
        startActivity(calculateCylinderUS);
    else if (prefs == false)
        startActivity(calculateCylinderMetric);
}

3 个答案:

答案 0 :(得分:1)

请勿按==符号

比较字符串变量

这里

if (preference == "us")
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference == "metric")
        menu.findItem(R.id.menu_metric).setChecked(true);

改为使用.equals()

if (preference.equals("us"))
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference.equals("metric"))
        menu.findItem(R.id.menu_metric).setChecked(true);

答案 1 :(得分:1)

请注意,在向用户显示菜单之前调用onPrepareOptionsMenu。 查看您的代码,您在setChecked(true)R.id.menu_us基于本地变量R.id.menu_metric调用prefs,其值始终true每当创建活动时。因此,为了保持这些状态,我们必须根据偏好值更新菜单项,而不是基于局部变量的值。

我认为您宁愿修改onPrepareOptionsMenuonCreateOptionsMenu函数,如下所示并尝试一下:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true; 
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference == "us")
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference == "metric")
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true;
}

答案 2 :(得分:1)

我从两个答案中都使用了一点点,我自己发现了一些缺失的部分。我发现的一个主要问题是,我没有“休息”;在我的switch语句中,它运行了两种情况,无论我选择哪一种。另一个最终修复不是调用

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference.equals("us"))
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference.equals("metric"))
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true; 

}

我的if / else if语句试图修改setChecked属性,我意识到我需要做的就是在onCreateOptionsMenu中修改“prefs”的值。像这样:

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference.equals("us"))
        prefs = true;
    else if (preference.equals("metric"))
        prefs = false;
    return true; 

}

onPrepareOptionsMenu仍然需要“setChecked”才能切换选择了哪个选项。 这是更新的工作代码:

public class MainActivity extends Activity {

boolean prefs = true;
String FILENAME = "settings";
String string;

public static final String LOG_TAG = "dbryant423";
public static final String PREFS_NAME = "settings";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(LOG_TAG, "prefs value onCreate: " +prefs);

 }

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String preference = settings.getString("preference", string);
    if (preference.equals("us"))
        prefs = true;
    else if (preference.equals("metric"))
        prefs=false;
    return true; 

}

// called when an item is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) { // decide which MenuItem was pressed based on it's id
    case R.id.menu_us:
            menuUS();
            break;
    case R.id.menu_metric:
            menuMetric();
            break;          
    }
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
    String preference = settings.getString("preference", string);
    if (preference.equals("us"))
        menu.findItem(R.id.menu_us).setChecked(true);
    else if (preference.equals("metric"))
        menu.findItem(R.id.menu_metric).setChecked(true);
    return true;
}

public void menuUS() {
    prefs = true;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME,   Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "us");
    editor.commit();
    Log.v(LOG_TAG, "prefs value menuUS: " +prefs);
}

public void menuMetric() {
    prefs = false;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("preference", "metric");
    editor.commit();
    Log.v(LOG_TAG, "prefs value menuMetric: " +prefs);
}

public void calculateCylinder(View v) {
    Intent calculateCylinderUS = new Intent(this, CalculateCylinder.class);
    Intent calculateCylinderMetric = new Intent(this, CalculateCylinderMetric.class);
     Log.v(LOG_TAG, "prefs value calculateCylinder: " +prefs);
    if (prefs == true)
        startActivity(calculateCylinderUS);
    else if (prefs == false)
        startActivity(calculateCylinderMetric);
}