OnBackPressed in NavigationView

3

I'm using the Android Studio template and adding this method to the main class:

@Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

Does anyone know what it is for?

    
asked by Red 04.06.2016 в 19:54
source

1 answer

0

When you perform a back press (press the% button% device%), if the NavigationView is open, closes it. If it is not open, execute Atrás that regularly closes the Activity.

@Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //esta abierto el NavigationView???
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            //Cierra el NavigationView.
            drawer.closeDrawer(GravityCompat.START);
        } else { //No esta abierto.
            //ejecuta onBackPressed().
            super.onBackPressed();
        }
    }
    
answered by 04.06.2016 / 20:16
source