[英]how to hide the item in Navigation drawer after one session is over
我的Activity
有一個包含三個項目的NavigationDrawer
。 我想在首次登錄時顯示所有三個項目。 在其他會話中,我想使一項不可見,並在NavigationDrawer
僅顯示兩項。
您必須使用此代碼來檢測應用程序的首次啟動
public class MyActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
}
}
}
在首次啟動時或在首次啟動完成后將1個項目添加到導航抽屜中,從導航抽屜中刪除其中1個項目
使用SharedPreferences存儲用戶狀態!
public class SharedPrefModel {
public static String INFO_STORE_TAG = "user_info";
public static String sharedPrefName = "USER";
private SharedPreferences sharedPref;
public SharedPrefModel(Context context) {
this.sharedPref = context.getSharedPreferences(sharedPrefName, MODE_PRIVATE);
}
public void setStatus(Boolean isFirstTime) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(INFO_STORE_TAG, isFirstTime);
editor.apply();
}
public Boolean getStatus() {
return sharedPref.getBoolean(INFO_STORE_TAG,false);
}
public void clearInfo() {
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.apply();
}
}
首次登錄后,將狀態設置為false。
new SharedPrefModel(this).setStatus(false);
下次檢查狀態是否為true。
if(!new SharedPrefModel(this).getStatus()){
//hide
}
重置狀態! 采用
new SharedPrefModel(this).clearInfo();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.