How can I set initial value of checkbox item part of a menu? When I start an Activity I want to set a boolean value saved in Shared Preferences.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity">
<item
android:id="@+id/checkbox"
android:title="@string/checkbox"
android:orderInCategory="10"
android:showAsAction="never"
android:visible="true"
android:checkable="true"
android:checked="true"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.checkbox){
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PrefsConsts.CHECKBOX,item.isChecked());
editor.commit();
}
return true;
}
I can always know what was the value, but I don´t know how to set the new one.