noob programmer
noob programmer

Reputation: 35

Navigation drawer is not closing even after closeDrawers()

I am creating a app with navigation drawer which is used to navigate through activities.

Here's my code or drawer

private void initInstances() {
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerToggle = new ActionBarDrawerToggle(busr.this, drawerLayout, R.string.hello_world, R.string.hello_world);
        drawerLayout.setDrawerListener(drawerToggle);
        drawerLayout.closeDrawers();

        navigation = (NavigationView) findViewById(R.id.navigation_view);
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                int id = menuItem.getItemId();
                switch (id) {
                    case R.id.navigation_item_1:
                        startActivity(new Intent(busr.this, MainActivity.class));
                        break;
                    case R.id.navigation_item_2:
                        startActivity(new Intent(busr.this, aff.class));
                        break;
                    case R.id.navigation_item_3:
                        startActivity(new Intent(busr.this, webs.class));
                        break;
                    case R.id.navigation_item_4:
                        startActivity(new Intent(busr.this, admnActivity.class));
                        break;
                    case R.id.navigation_item_5:
                        startActivity(new Intent(busr.this, busr.class));
                        break;
                    case R.id.navigation_item_6:
                        startActivity(new Intent(busr.this, trng.class));
                        break;
                    case R.id.navigation_item_7:
                        startActivity(new Intent(busr.this, prospct.class));
                        break;
                    case R.id.navigation_item_8:
                        startActivity(new Intent(busr.this, erp.class));
                        break;
                    case R.id.navigation_item_9:
                        startActivity(new Intent(busr.this, result.class));
                        break;
                }
                return false;
            }
        });

Upvotes: 0

Views: 102

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this: For closing the drawerlayout

case R.id.navigation_item_1:
                    if (drawerLayout.isDrawerOpen(Gravity.START))
                        drawerLayout.closeDrawer(Gravity.START);
                    startActivity(new Intent(busr.this, MainActivity.class));
                    break;

or

case R.id.navigation_item_1:
                    drawerLayout.closeDrawers();
                    startActivity(new Intent(busr.this, MainActivity.class));
                    break;

Upvotes: 1

Related Questions