您必须将数据保存在某处。在这种情况下,我建议使用Shared Preferences这是存储简单的布尔值和其他小数据的首选方法,每个数据都有一个特定的键。在你的情况下,一旦用户点击该菜单项,调用
// Where this is a Context such as your Activity
SharedPreferences sp = PreferenceManager.getSharedPreferences(this);
sp.edit().putBoolean("MENU_CLICK", true).apply();
// Use commit() in place of apply() if you support pre-Gingerbread devices
然后在你的onPrepareOptionsMenu()
,通过使用
SharedPreferences sp = PreferenceManager.getSharedPreferences(this);
// default false to show on first run
final boolean haveClickedMenu = sp.getBoolean("MENU_CLICK", false);
mi.setEnabled(!haveClickedMenu);
线索检索共享偏好 - sharedPreferences。 Google是你的朋友。 – Simon