Search code examples
androidnavigation-drawerdrawerlayout

How to make drawer just slide close instead of creating the activity again?


I have an application where onItemClick() activity determines which item in the drawer is clicked and the corresponding activity is started. However, when if I am already at the 'Settings' screen and I open the drawer and click on 'Settings' again, it starts a new settings activity. How can I optimize my code here to just detect that user was already on the Settings screen and therefore the drawer should just slide close? Here's my code:

//@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    Drawer.closeDrawers();
    Intent i = new Intent(DrawerActivity.this, SignUpActivity.class);
    switch(position){
        case 7:
            i = new Intent(DrawerActivity.this, UserSettingActivity.class);
            break;
    }
    startActivity(i);

Solution

  • For that you can store that last clicked number in one variable.

    Then next time do check whether current clicked position is not equal to last store position

    If it's not equal launch new screen and updated last clicked position else don't do nothing