@Override public void onBackPressed() { if (navigationDrawer.isDrawerOpen(GravityCompat.START)) { navigationDrawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mojo_menu); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar supportActionBar = getSupportActionBar(); if (supportActionBar != null) { supportActionBar.setTitle(R.string.nav_menu_title); } navigationDrawer = (DrawerLayout) findViewById(R.id.mojo_menu_drawer_layout); navigationDrawerToggle = new ActionBarDrawerToggle( this, navigationDrawer, toolbar, R.string.action_open_navigation_drawer, R.string.action_close_navigation_drawer) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (navigationView.getMenu().findItem(R.id.nav_orders).isChecked()) { ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation( MojoMenuActivity.this, toolbar, getString(R.string.toolbar_transition)); MojoOrderActivity.start( MojoMenuActivity.this, orderItemList, optionsCompat.toBundle()); } else if (navigationView.getMenu().findItem(R.id.nav_settings).isChecked()) { SettingsActivity.start(MojoMenuActivity.this); } navigationView.setCheckedItem(R.id.nav_menu); } }; navigationDrawer.addDrawerListener(navigationDrawerToggle); navigationView = (NavigationView) findViewById(R.id.mojo_menu_nav_view); navigationView.setCheckedItem(R.id.nav_menu); navigationView.setNavigationItemSelectedListener(this); goToCartButton = (Button) findViewById(R.id.bottom_action_button); goToCartButton.setOnClickListener(this); tabLayout = (TabLayout) findViewById(R.id.mojo_menu_tab_layout); viewPager = (ViewPager) findViewById(R.id.mojo_menu_view_pager); baseReference = FirebaseDatabase.getInstance().getReference().child("data"); mojoDataChildEventListener = new MojoDataChildEventListener(); if (savedInstanceState != null) { mojoData = savedInstanceState.getParcelable(EXTRA_MOJO_DATA); orderItemList = savedInstanceState.getParcelableArrayList(EXTRA_ORDER_ITEM_LIST); updateTabLayoutAndViewPager(); updateGoToCartButton(); } else { mojoData = new MojoData(); orderItemList = new ArrayList<>(); showLoadingDialog(); baseReference.addListenerForSingleValueEvent( new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { dismissLoadingDialog(); if (dataSnapshot != null && dataSnapshot.exists()) { ArrayList<MojoMenu> mojoMenuList = new ArrayList<>(); ArrayList<Topping> toppingList = new ArrayList<>(); ArrayList<String> availableZipList = new ArrayList<>(); Map<String, Map<String, Object>> mojoDataMap = (Map<String, Map<String, Object>>) dataSnapshot.getValue(); Map<String, Object> toppingDataMap = mojoDataMap.get("topping"); for (Object toppingData : toppingDataMap.values()) { toppingList.add(new Topping((Map<String, Object>) toppingData)); } Map<String, Object> menuDataMap = mojoDataMap.get("menu"); for (Object menuData : menuDataMap.values()) { mojoMenuList.add(new MojoMenu((Map<String, Object>) menuData)); } availableZipList.addAll((List<String>) mojoDataMap.get("zip")); mojoData.setMojoMenuList(mojoMenuList); mojoData.setToppingList(toppingList); mojoData.setAvailableZipList(availableZipList); updateTabLayoutAndViewPager(); } } @Override public void onCancelled(DatabaseError databaseError) { dismissLoadingDialog(); Log.e("findme", "Load menu data failed: " + databaseError.getMessage()); } }); } }
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); navigationDrawerToggle.onConfigurationChanged(newConfig); }
@Override protected void onSaveInstanceState(Bundle outState) { outState.putParcelable(EXTRA_MOJO_DATA, mojoData); outState.putParcelableArrayList(EXTRA_ORDER_ITEM_LIST, orderItemList); super.onSaveInstanceState(outState); }
@Override protected void onStop() { baseReference.removeEventListener(mojoDataChildEventListener); super.onStop(); }
@Override protected void onStart() { super.onStart(); baseReference.addChildEventListener(mojoDataChildEventListener); }
@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); navigationDrawerToggle.syncState(); }