Thomas Thomas - 1 year ago 32
Android Question

Storing Multiple Menu Item State Using Shared Preferences

I am reposting this question since I am still stuck at this.

So I am creating a simple android application. I have a menu item which has 3 check boxes in it. As soon as my app starts, the user can select one or more from the given check boxes.but, I want to save the user selection for further reference. I achieved this for 1 of the check boxes using shared preferences but I am unable to achieve this if more than 1 check boxes are selected. Here is my code:

public class MainActivity extends AppCompatActivity {
static Switch personalizationSwitch;
static TextView onOffBar;
public static boolean personalizePermissions = false;
public static boolean translucentLayerOn = false;
public static boolean flowInfoOn = false;
private static int tasknr = 0;

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onOffBar = (TextView) findViewById(R.id.onOffBar_text);
personalizationSwitch = (Switch) findViewById(R.id.personalisation_switch); //To test
personalizationSwitch.setChecked(false);
personalizationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onOffBar.setText(isChecked ? R.string.on : R.string.off);
if (isChecked) {
askToStartAccessibilityService(getResources().getString(R.string.on));
} else {
askToStartAccessibilityService(getResources().getString(R.string.off));
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//create the options menu where flow information, personalization, obfuscation can be activated or the accessibility page visited easily
getMenuInflater().inflate(R.menu.main_menu, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);

MenuItem item = menu.findItem(R.id.checkbox_personalize_permissions);
// MenuItem item1 = menu.findItem(R.id.checkbox_add_tanslucent_layer);
// MenuItem item2 = menu.findItem(R.id.checkbox_integrate_flowInfo);
item.setChecked(isChecked);
// item1.setChecked(isChecked);
// item2.setChecked(isChecked);
//return super.onCreateOptionsMenu(menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.checkbox_personalize_permissions:
item.setChecked(!item.isChecked());
//personalizePermissions = item.isChecked();
// SharedPreferences settings = getSharedPreferences("settings", 0);
// SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.checkbox_add_tanslucent_layer:
item.setChecked(!item.isChecked());
//translucentLayerOn = item.isChecked();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.checkbox_integrate_flowInfo:
item.setChecked(!item.isChecked());
//flowInfoOn=item.isChecked();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.item_accessibility_services:
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
return true;
default:
return super.onOptionsItemSelected(item);
}
}


How do I achieve this for all the switch case options using shared preferences?

Thanks.

Answer Source

Update your two methods - onCreateOptionsMenu and onOptionsItemSelected as shown below.

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
        //create the options menu where flow information, personalization, obfuscation can be activated or the accessibility page visited easily
        getMenuInflater().inflate(R.menu.main_menu, menu);
        SharedPreferences settings = getSharedPreferences("settings", 0);
        boolean personalizeCheckbox = settings.getBoolean("personalizeCheckbox", false);
        boolean translucentCheckbox = settings.getBoolean("translucentCheckbox", false);
        boolean flowCheckbox = settings.getBoolean("flowCheckbox", false);

        MenuItem item = menu.findItem(R.id.checkbox_personalize_permissions);
        MenuItem item1 = menu.findItem(R.id.checkbox_add_tanslucent_layer);
        MenuItem item2 = menu.findItem(R.id.checkbox_integrate_flowInfo);
        item.setChecked(personalizeCheckbox);
        item1.setChecked(translucentCheckbox);
        item2.setChecked(translucentCheckbox);
        //return super.onCreateOptionsMenu(menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        SharedPreferences settings = getSharedPreferences("settings", 0);
        SharedPreferences.Editor editor = settings.edit();
        switch (item.getItemId()) {
            case R.id.checkbox_personalize_permissions:
                item.setChecked(!item.isChecked());
                //personalizePermissions = item.isChecked();
//                SharedPreferences settings = getSharedPreferences("settings", 0);
//                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("personalizeCheckbox", item.isChecked());
                editor.commit();
                return true;
            case R.id.checkbox_add_tanslucent_layer:
                item.setChecked(!item.isChecked());
                //translucentLayerOn = item.isChecked();
                editor.putBoolean("translucentCheckbox", item.isChecked());
                editor.commit();
                return true;
            case R.id.checkbox_integrate_flowInfo:
                item.setChecked(!item.isChecked());
                //flowInfoOn=item.isChecked();
                editor.putBoolean("flowCheckbox", item.isChecked());
                editor.commit();
                return true;
            case R.id.item_accessibility_services:
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download