diff --git a/src/it/reyboz/bustorino/ActivityPrincipal.java b/src/it/reyboz/bustorino/ActivityPrincipal.java index 52b199d..bba046d 100644 --- a/src/it/reyboz/bustorino/ActivityPrincipal.java +++ b/src/it/reyboz/bustorino/ActivityPrincipal.java @@ -1,100 +1,113 @@ package it.reyboz.bustorino; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.widget.Toolbar; import androidx.core.app.NavUtils; +import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import com.google.android.material.navigation.NavigationView; import it.reyboz.bustorino.middleware.GeneralActivity; public class ActivityPrincipal extends GeneralActivity { private DrawerLayout mDrawer; private NavigationView mNavView; private ActionBarDrawerToggle drawerToggle; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_main_activity); Toolbar mToolbar = findViewById(R.id.default_toolbar); setSupportActionBar(mToolbar); //getSupportActionBar().setDisplayHomeAsUpEnabled(true); mDrawer = findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(mToolbar); // Setup toggle to display hamburger icon with nice animation drawerToggle.setDrawerIndicatorEnabled(true); drawerToggle.syncState(); mNavView = findViewById(R.id.nvView); setupDrawerContent(mNavView); } private ActionBarDrawerToggle setupDrawerToggle(Toolbar toolbar) { - // NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it - // and will not render the hamburger icon without it. - return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( menuItem -> { //selectDrawerItem(menuItem); return true; }); } // `onPostCreate` called when activity start-up is complete after `onStart()` // NOTE 1: Make sure to override the method with only a single `Bundle` argument // Note 2: Make sure you implement the correct `onPostCreate(Bundle savedInstanceState)` method. // There are 2 signatures and only `onPostCreate(Bundle state)` shows the hamburger icon. @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. drawerToggle.syncState(); } @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles drawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()){ + case android.R.id.home: + mDrawer.openDrawer(GravityCompat.START); + return true; + + } + if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } + + @Override + public void onBackPressed() { + if (mDrawer.isDrawerOpen(GravityCompat.START)) + mDrawer.closeDrawer(GravityCompat.START); + else + super.onBackPressed(); + } }