The menu button in the upper left corner of the application does not work after I switch to another action in the menu.

When the application starts, one of the application fragments is set by default, and at the moment, if you click on the button that brings up the menu, the menu will come out from the left. After, if you switch to another fragment in this menu, then the button does not respond to pressing and the menu from the left does not climb, but! Swipe from left - to - right is invoked.

Those. I would like to understand why this is so and where is the problem?

The application itself was created from a template in AndroidStudio

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/my_audios" android:icon="@drawable/ic_my_audio" android:title="Мои аудиозаписи" android:checked="true" /> <item android:id="@+id/popular_audios" android:icon="@drawable/ic_popular_audio" android:title="Популярные" /> <item android:id="@+id/recommend_audios" android:icon="@drawable/ic_recomendation_audio" android:title="Рекомендуемые" /> </group> <item android:title="Проигрыватель"> <menu> <!--item android:id="@+id/clear_cash" android:icon="@android:drawable/ic_menu_share" android:title="Отчистить кэш" /--> <item android:id="@+id/item_player" android:icon="@drawable/ic_player_audio" android:title="Открыть" /> <!--item android:id="@+id/exit_from_profile" android:icon="@android:drawable/ic_menu_send" android:title="Выйти из профиля" /--> </menu> </item> </menu> 

MainActivity

 SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar); // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.my_audios) { MyAudioFragment fragment = new MyAudioFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame, fragment); fragmentTransaction.commit(); getSupportActionBar().setTitle("Мои аудиозаписи"); //return true; } else if (id == R.id.popular_audios) { PopularAudioFragment fragment = new PopularAudioFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame, fragment); fragmentTransaction.commit(); getSupportActionBar().setTitle("Поплуярные аудиозаписи"); } else if (id == R.id.recommend_audios) { RecommendationAudioFragment fragment = new RecommendationAudioFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame, fragment); fragmentTransaction.commit(); getSupportActionBar().setTitle("Рекомендуемые аудиозаписи"); // } else if (id == R.id.clear_cash) { //} else if (id == R.id.exit_from_profile) { } else if (id == R.id.item_player) { PlayerFragment fragment = new PlayerFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame, fragment); fragmentTransaction.commit(); getSupportActionBar().setTitle("Проигрыватель"); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 

UPD:

 @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } 
  • Add the onOptionItemSelected implementation to the question and Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar); Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar); in onNavigationItemSelected not needed. Transfer it to onCreate - Yuriy SPb
  • I did not understand at all. - or_die
  • The driver is opened in the handler for clicking on the menu items in the activation. - Yuriy SPb
  • Again not monyali. I like only a newcomer to this. - or_die
  • The code that led to your problem is not relevant. The door is usually opened by pressing the hamburger icon (three horizontal bars). Handle clicking on this button in the onOptionsItemSelected() method. Give the code for this method in question. - Yuriy SPb

1 answer 1

Add this to the onOptionsItemSelected method. This code handles clicking on the hamburger icon and opens / closes the drover:

 if (id == android.R.id.home) { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { drawer.openDrawer(GravityCompat.START); } return true; } 

And free advice - do not use templates while they cause questions. Those. it is easier to study how it works by reading individual articles on drover, fragments, etc.

  • I can not find this icon. R.id.menu - there is no such ID - or_die
  • @or_die, yes, I didn’t enter the ID. See the updated answer. - Yuriy SPb
  • Thank !!!!!!!!!!!!! ) - or_die
  • 2
    @or_die, please) And in fact - drive into the templates - they only have a use if you know exactly all the nuances. Otherwise, if something is not in the template implemented from the right, you will be tortured to edit it to fit your needs. Easier to write from scratch. At the same time you will understand the subtleties) - YuriySPb
  • Understood, thank you!) - or_die