您必須將數據保存在某處。在這種情況下,我建議使用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