1

I have a small problem with navigation drawer. I want to have nothing "checked" after return to main activity, here is more about my problem:

When I am starting an activity, I have menu like that: enter image description here

That is good :) When I click "User profil" or other menu item I am opening new activity. I don't have any menu in this "new activity", only return button, so I am returning to main activity. And here is a problem, when I return to main activity and I open navigation drawer, it is looking like:

enter image description here

What I have to do to uncheck this menu item that I previous open?

//EDIT Code of Navigation Drawer:

mToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    ActionBar actionbar = this.getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);

    mDrawerLayout = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    // close drawer when item is tapped
                    mDrawerLayout.closeDrawers();
                    switch (menuItem.getItemId()) {
                        case android.R.id.home:
                            mDrawerLayout.openDrawer(GravityCompat.START);
                            return true;
                        case R.id.action_user:
                            //Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
                            choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
                            return true;
                        case R.id.action_settings:
                            choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
                            return true;
                        case R.id.action_about:
                            choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
                            return true;
                        case R.id.action_log_out:
                            choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
                            return true;
                            default:
                                choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
                    }

                    //UNCHECK? - NO WORKING
                    if(menuItem.isChecked()){
                        menuItem.setChecked(false);
                    }
                    return true;

                }
            });

    mDrawerLayout.addDrawerListener(
            new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset) {
                    // Respond when the drawer's position changes
                }

                @Override
                public void onDrawerOpened(View drawerView) {
                    // Respond when the drawer is opened
                    choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    // Respond when the drawer is closed
                    switch (choseIntentFromDrawerLayout) {
                        case EventContract.EventEntry.MENU_USER_PROFIL:
                            Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
                            mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
                            startActivity(intentUser);
                            break;
                        case EventContract.EventEntry.MENU_SETTINGS:
                            Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
                            startActivity(intentSettings);
                            break;
                        case EventContract.EventEntry.MENU_ABOUT:
                            Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
                            startActivity(intentAbout);
                            break;
                        case EventContract.EventEntry.MENU_LOG_OUT:
                            AuthUI.getInstance().signOut(CatalogActivity.this);
                            break;
                        case EventContract.EventEntry.MENU_NOTHING:
                            break;
                    }

                }

                @Override
                public void onDrawerStateChanged(int newState) {
                    // Respond when the drawer motion state changes
                }
            }
    );
Panicum
  • 794
  • 1
  • 9
  • 32
  • 1
    How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity. – giro Nov 14 '18 at 23:16
  • With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility? – Panicum Nov 14 '18 at 23:19
  • 1
    Could you post the code related to your menu? – giro Nov 14 '18 at 23:21
  • I added it to the previous post. – Panicum Nov 14 '18 at 23:29
  • 1
    As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: https://stackoverflow.com/questions/36051045/how-to-uncheck-checked-items-in-navigation-view – giro Nov 14 '18 at 23:45
  • Thank you. It is working, but I put this function, that is unchecking all menu items to my onDrawerClosed listener. – Panicum Nov 14 '18 at 23:57

2 Answers2

1

I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View? It is also possible to uncheck menu items in onDrawerClosed listner: This code works:

@Override
                public void onDrawerClosed(View drawerView) {
                    //Solution:
                    int size = navigationView.getMenu().size();
                    for (int i = 0; i < size; i++) {
                        navigationView.getMenu().getItem(i).setChecked(false);
                    }
                    // Respond when the drawer is closed
                    switch (choseIntentFromDrawerLayout) {
                        case EventContract.EventEntry.MENU_USER_PROFIL:
                            Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
                            mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
                            startActivity(intentUser);
                            break;
                        case EventContract.EventEntry.MENU_SETTINGS:
                            Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
                            startActivity(intentSettings);
                            break;
                        case EventContract.EventEntry.MENU_ABOUT:
                            Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
                            startActivity(intentAbout);
                            break;
                        case EventContract.EventEntry.MENU_LOG_OUT:
                            AuthUI.getInstance().signOut(CatalogActivity.this);
                            break;
                        case EventContract.EventEntry.MENU_NOTHING:
                            break;
                    }

                }
Mozahler
  • 4,958
  • 6
  • 36
  • 56
giro
  • 421
  • 1
  • 7
  • 19
1

Firstly, Let's create an int variable to get the size of your navigation menu.

int size = navigationView.getMenu().size();

Second, make a for loop to un-check all the menu items in your navigation view

for (int i = 0; i < size; i++)
        navigationView.getMenu().getItem(i).setChecked(false);

Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.

navigationView.setNavigationItemSelectedListener(
        new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                // close drawer when item is tapped
                int size = navigationView.getMenu().size();
                for (int i = 0; i < size; i++)
                navigationView.getMenu().getItem(i).setChecked(false);
                //Your switch code here.
                }

Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.

MinnKhant
  • 61
  • 8