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); }
onOptionItemSelectedimplementation to the question andToolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar);Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mActionBarToolbar);inonNavigationItemSelectednot needed. Transfer it toonCreate- Yuriy SPb ♦onOptionsItemSelected()method. Give the code for this method in question. - Yuriy SPb ♦