How to close Drawer layout on BackPress in Android?

前端 未结 4 759
无人共我
无人共我 2020-12-03 08:09

I press the navigation drawer, then if I press back button, the app exits rather than returning to the previous activity. If I change the xml file, then this problem doesn\'

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 09:03

    This will close the drawer when it's open and back is pressed rather than taking you back to the previous activity (or exiting).

    DrawerLayout drawer...
    
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
    
        if(drawer.isDrawerOpen(Gravity.LEFT)){
            drawer.closeDrawer(Gravity.LEFT);
        }else{
            super.onBackPressed();
        }
    }
    

提交回复
热议问题