嗨我设计了一个自定义工具栏,用弹出菜单替换操作栏,使用提示
how to save menuitem visibility state through sharedpreferences?
和
Checkbox item state on menu android
和
http://developer.android.com/guide/topics/ui/menus.html#checkable
最有效的方法是将状态存储在共享首选项中,如*答案中所示.
我的问题是:即使重新启动我的Android后,如何保持选中的选项?
解决方法:
一种方法是在.commit()之前调用.clear()方法.
另一种方法是检索共享首选项中的最后一个存储值.但是,要做到这一点,必须了解活动的生命周期:
存储已检查的用户首选项状态后,如下所示:
声明变量
/**Checkable Login Persist Shared Prefs Declarations Start*/
private static final String PREFS_NAME = "IsCheckedState";
String string;
SharedPreferences.Editor editor;
/**Checkable Login Persist Shared Prefs Declarations End*/
设置布局后,将字符串(无论您的情况可能是boolean或int)变量分配给共享首选项.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
Persistent Checkable Menu Start
**/
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
//Log.e("User Subscription", string);
/**
Persistent Checkable Menu End
**/}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SharedPreferences settings = this.getSharedPreferences(PREFS_NAME, 0);
string = settings.getString("preference", string);
if (string.equals("Vibrate")) {
menu.findItem(R.id.start_action).setChecked(true);
Log.e("Vibrate", string);
}
else if (string.equals("Disable")){
menu.findItem(R.id.my_cancel_action).setChecked(true);
Log.e("Disable", string);
}
return true;
}
// Menu options to set and cancel the alarm.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*persistent checkable item logic start*/
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
editor = settings.edit();
string = settings.getString("preference", string);
/*persistent checkable item logic end*/
int id = item.getItemId();
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (id) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm();
item.setChecked(true);
string= "Vibrate";
editor.putString("preference", string);
editor.commit();
// invalidateOptionsMenu();
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.my_cancel_action:
alarm.cancelAlarm(this, 1);
item.setChecked(true);
string="Disable";
editor.putString("preference", "Disable");
editor.commit();
//invalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
当应用程序暂停,停止,恢复可见(启动)时,您必须从共享首选项中检索以前存储的字符串.因此,除了上面的代码,您还需要添加以下内容:
}
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
@Override
protected void onRestart() {
super.onRestart();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
string = settings.getString("preference", string);
}
也就是说,如果您确实希望始终存储已选中的选项.