Search code examples
androidandroid-menuandroid-navigationandroid-drawer

How to close the Navigation drawer in android


My application has a navigation drawer. From drawer options, I am opening different activities. At that time a blank screen is displayed before the new Activity.

@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
    case R.id.nav_message:

        Intent newAct = new Intent(getApplicationContext(), FeedbackActivity.class);
        startActivity(newAct);
    
       break;


}


drawer.closeDrawer(Gravity.RIGHT);
return true;

}

As per some investigations, I found like we need to remove the below code from this. Tried the same and working fine.

drawer.closeDrawer(Gravity.RIGHT);
    return true;

But when I click the back button from the new activity, the drawer is still open status. How can I close without that black screen?


Solution

  • Have you tried passing GRAVITY.START as a parameter to drawer.close(int gravity), like this:

    switch (item.getItemId()) {
        case R.id.nav_message:
    
            Intent newAct = new Intent(getApplicationContext(), FeedbackActivity.class);
            startActivity(newAct);
        
           break;
    
    
    }
    
    
    
    drawer.closeDrawer(GravityCompat.START);
    return true;
    

    And don't forget to initialize the drawer object with findViewById()