简体   繁体   中英

how to hide the item in Navigation drawer after one session is over

I have a NavigationDrawer in my Activity with three items. I want to show all three items when first time user Login. In other session I want to make one item invisible and show only two items in NavigationDrawer .

you have to detect first launch of the app using this code

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();
    }
}
}

add 1 item to navigation drawer at first launch or after first launch is completed, remove your 1 of the item from navigation drawer

USE SharedPreferences to store user status!

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();
    }
}

After login for the first time set the status to false.

new SharedPrefModel(this).setStatus(false);

Next Time check that if the status is true or not.

if(!new SharedPrefModel(this).getStatus()){
//hide
}

to reset the status! use

new SharedPrefModel(this).clearInfo();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM