4

I have used navigation drawer in my app. When user click any of its item it will go to another activity but when user comes back to the drawer page it is still open. How to close that drawer once its item is clicked.

Piyush jain
  • 83
  • 1
  • 1
  • 6
  • close drawer before calling intent. for more help please post your code. – V-rund Puro-hit Jul 29 '16 at 07:17
  • i have posted solution try it. if still not solved then post your code here – Vishal Thakkar Jul 29 '16 at 07:31
  • Possible duplicate of [How to close navigation drawer when an item is pressed from it?](http://stackoverflow.com/questions/19194526/how-to-close-navigation-drawer-when-an-item-is-pressed-from-it) – Preet_Android Jul 29 '16 at 07:47
  • Use the instance of DrawerLayout and call this function DrawerLayout.closeDrawers(); – Hassan Jamil Jul 29 '16 at 12:26
  • i am using a separate java file for handling the functionality of the drawer, and the problem is that java file don't have drawerlayout in its xml so i am not able to get the reference of the drawer there. – Piyush jain Aug 01 '16 at 05:45

7 Answers7

4

You can use drawerLayout.closeDrawers(); to close the NavigationDrawer before you go to another Activity

Linh
  • 57,942
  • 23
  • 262
  • 279
3
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout)

closeDrawer(); // called when you want to close

  public void closeDrawer() 
  {
     if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
     {
        mDrawerLayout.closeDrawer(GravityCompat.START);
     }
 }
Ashif
  • 441
  • 5
  • 12
2

Use closeDrawer() method to close the drawer and start your other activity on the listener of drawer.

For Example.

@Override
public void onDrawerClosed(View drawerView) {
    super.onDrawerClosed(drawerView);

    //Start your activity
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21
0

I think you missed closeDrawer() just call this method before launching other on click of Navigation Item Intent

 drawerLayout.closeDrawer(GravityCompat.END);
N J
  • 27,217
  • 13
  • 76
  • 96
0
 @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) { 
int id = item.getItemId();
    // Create a new fragment and specify the fragment to show based on nav item clicked
    Fragment fragment = null;
    Class fragmentClass = null;
    if (id == R.id.nav_item1) {
        fragmentClass = home.class;
        // Handle the camera action
    } 
 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
if(fragmentClass.newInstance() instanceof Fragment) {
            fragment = (Fragment) fragmentClass.newInstance();

            if (fragmentClass.getSimpleName().equals("home")) {

                // Insert the fragment by replacing any existing fragment
                FragmentManager fragmentManager = getSupportFragmentManager();

                fragmentManager.beginTransaction().replace(R.id.home_layout, fragment).commit();
                // Highlight the selected item has been done by NavigationView
                item.setChecked(true);
                // Set action bar title
                setTitle(item.getTitle());
                // Close the navigation drawer
                drawer.closeDrawers();
            }
        }
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

Just add closeDrawer() inside onNavigationItemSelected() to close the drawer on selecting any item on Navigation Drawer.

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    //write your if logic here

    drawer.closeDrawer(GravityCompat.START,true);
    return false;
}
Sonu Sourav
  • 2,926
  • 2
  • 12
  • 25
-1

Probably the navigation view is not in the front. Try getting it to front by mNavigationView.bringToFront();.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Pavan K
  • 1
  • 2