Search code examples
androidnavigation-drawerandroid-sharedpreferences

Navigation Drawer Refreshed


I am following this answer on SO, which is titled as:

Change NavigationView items when user is logged

The code works fine but the content of NavigationView change when I restart the app. I want the content to be changed after I click Login or LogOut item menus

This is my code in onCreate() method:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        if(islogin())
        {
            navigationView.getMenu().clear();
            navigationView.inflateMenu(R.menu.activity_main_drawer1);
        } else
        {
            navigationView.getMenu().clear();
            navigationView.inflateMenu(R.menu.activity_main_drawer2);
        }
        navigationView.setNavigationItemSelectedListener(this);
        toggle.syncState();

and here is the islogin() method:

    public boolean islogin(){
// Retrieve data from preference:
        prefs = getSharedPreferences("UserLoginData", MODE_PRIVATE);
        String username = prefs.getString("username",   null);
        if (username == null) {
            return false;
        }
        else{
            return true;
        }
    }

Any help would greatly appreciated! Thank You

Note: Though this question seems duplicate of some, but its just the title, contents are entirely different.


Solution

  • Though I didn't get answer, I am posting my solution here. I did solve it a very simple logic and its working very great.

    Step 1:

    I first initialize Toolbar toolbar; globally above class.

    Step 2:

    Then I just create a simple method called: myDrawer() and wrap all my Drawer code inside it. Like this:

    public void myDrawer(){
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
    
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    
        if(islogin())
        {
            navigationView.getMenu().clear();
            navigationView.inflateMenu(R.menu.activity_main_drawer2);
        } else
        {
            navigationView.getMenu().clear();
            navigationView.inflateMenu(R.menu.activity_main_drawer);
        }
        navigationView.setNavigationItemSelectedListener(this);
        toggle.syncState();
    
    }
    

    Now I have hands over the Navigation and I can do anything with it, Like Refreshing and Calling again etc.

    Step 3:

    I am calling the method in Main Activity i-e: OnCreate

    myDrawer();
    

    Step 4:

    And I am calling it every time I do logic of Signin and SignOut.

    Wow! It's working like a charm.

    PS: Just for the info purpose, Here is my onNavigationItemSelected, where I can handle Click Events:

    public boolean onNavigationItemSelected(MenuItem item) {
    
            int id = item.getItemId();
            Intent intent;
            if (id == R.id.nav_item_item1) {
    
                intent = new Intent(MainActivity.this, SomeClass1.class);
                startActivity(intent);
    
            } else if (id == R.id.nav_item_item2) {
                intent = new Intent(getApplicationContext(), SomeClass2.class);
                startActivity(intent);
    
            } else if (id == R.id.nav_item_logout) {
                // my other logic for signout
                   myDrawer();
    
            } 
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    

    Credits:

    User: manish jain Answer