There is Activity causing Fragment s alternately - 3 pieces. The Activity uses Mike Penz's MaterialDrawer . In the fragments, by pressing the Back button in Tollbar , Drawer 'a is called, that is, you can return to the previous fragment by pressing only the Physical button on the phone itself. Is there any possibility to override the functionality of the button that appears by calling ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

UPD Activity code itself

 public class MainActivity extends AppCompatActivity implements KitchenFragment.CallbackOne { public static final String TAG = "myLogTag"; private Toolbar mToolbar; private Drawer drawer; private FragmentManager fm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(false); fm = getFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.content_frame); if (fragment == null) { fragment = MenuFragment.newInstance(); fm.beginTransaction() .add(R.id.content_frame, fragment) .commit(); } drawer = new DrawerBuilder() .withActivity(this) .withToolbar(mToolbar) .withActionBarDrawerToggle(true) .withHeader(R.layout.drawer_header) .addDrawerItems( new PrimaryDrawerItem().withName(R.string.menu).withIdentifier(1), new PrimaryDrawerItem().withName(R.string.kitchen_title).withIdentifier(2), new PrimaryDrawerItem().withName(R.string.information_title).withEnabled(false) ).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { Log.d(TAG, "position clicked: " + position); Fragment fragment = MenuFragment.newInstance(); switch (position) { case 1: fragment = MenuFragment.newInstance(); break; case 2: fragment = KitchenFragment.newInstance(); break; default: fragment = new Fragment(); } fm.beginTransaction().replace(R.id.content_frame, fragment).commit(); return false; } }) .withFireOnInitialOnClick(true) .withSavedInstance(savedInstanceState) .withOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() { @Override public boolean onNavigationClickListener(View view) { Log.d(TAG, "CLICK"); if (!drawer.getActionBarDrawerToggle().isDrawerIndicatorEnabled()) { onBackPressed(); return true; } else return false; } }) .build(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.d(TAG, "CLCK!"); switch (item.getItemId()) { case R.id.action_settings: return true; case android.R.id.home: getFragmentManager().popBackStackImmediate(); return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onSaveInstanceState(Bundle outState) { outState = drawer.saveInstanceState(outState); super.onSaveInstanceState(outState); } @Override public void onBackPressed() { if (drawer.isDrawerOpen()) drawer.closeDrawer(); else if (getFragmentManager().getBackStackEntryCount() == 1) { getSupportActionBar().setDisplayHomeAsUpEnabled(false); drawer.getActionBarDrawerToggle().syncState(); getFragmentManager().popBackStack(); } else if (getFragmentManager().getBackStackEntryCount() > 0) getFragmentManager().popBackStack(); else super.onBackPressed(); } @Override public void setFirstSelected() { drawer.setSelection(1); } } 

1 answer 1

Based on the source code of the library that the author specified ( MaterialDrawer ), we need to install the navigation handler used by the library to install onNavigationClickListener in the Toolbar . It looks like this:

 public interface OnDrawerNavigationListener { /** * @param clickedView * @return true if the event was consumed */ boolean onNavigationClickListener(View clickedView); } 

It is installed when the Drawer is built from DrawerBuilder as follows:

  /** * handles the different logics for the Drawer Navigation Listeners / Indications (ActionBarDrawertoggle) */ protected void handleDrawerNavigation(Activity activity, boolean recreateActionBarDrawerToggle) { //set the navigationOnClickListener final View.OnClickListener toolbarNavigationListener = new View.OnClickListener() { @Override public void onClick(View v) { boolean handled = false; if (mOnDrawerNavigationListener != null && (mActionBarDrawerToggle != null && !mActionBarDrawerToggle.isDrawerIndicatorEnabled())) { handled = mOnDrawerNavigationListener.onNavigationClickListener(v); } if (!handled) { if (mDrawerLayout.isDrawerOpen(mDrawerGravity)) { mDrawerLayout.closeDrawer(mDrawerGravity); } else { mDrawerLayout.openDrawer(mDrawerGravity); } } } }; ... 

Further in the same method:

 //if we got a toolbar set a toolbarNavigationListener //we also have to do this after setting the ActionBarDrawerToggle as this will overwrite this if (mToolbar != null) { this.mToolbar.setNavigationOnClickListener(toolbarNavigationListener); } 

Find the ability to install mOnDrawerNavigationListener in DrawerBuilder.java:

 /** * Define a OnDrawerNavigationListener for this Drawer * * @param onDrawerNavigationListener * @return this */ public DrawerBuilder withOnDrawerNavigationListener(@NonNull Drawer.OnDrawerNavigationListener onDrawerNavigationListener) { this.mOnDrawerNavigationListener = onDrawerNavigationListener; return this; } 

The condition (mActionBarDrawerToggle != null && !mActionBarDrawerToggle.isDrawerIndicatorEnabled()) is executed by default:

 // enable the drawer toggle / if withActionBarDrawerToggle we will autoGenerate it protected boolean mActionBarDrawerToggleEnabled = true; 

As a result, the initialization will look something like this:

 Drawer result = new DrawerBuilder() .withActivity(this) .withToolbar(toolbar) .withOnDrawerNavigationListener(new OnDrawerNavigationListener() { @Override public boolean onNavigationClickListener(View clickedView) { // реализация навигации (.popBackStack(), finish() и тд) return false; } }) .addDrawerItems( //pass your items here ) .build(); 

If the navigation handler returns false, processing can be performed in the standard method:

     @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case android.R.id.home:                onBackPressed(); // или другое действие                return true;            default:                return super.onOptionsItemSelected(item);        }    } 

The author of the library also indicated the ability to change the navigation icon in another way.

Show the back arrow icon as the Up button:

 result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

And to return the "hamburger" (side menu icon) to the place Up :

 getSupportActionBar().setDisplayHomeAsUpEnabled(false); result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true); 
  • It did not help, because the click handler does not respond if this library is used - abbath0767
  • @ mamba0767 updated the answer, please let me know if that didn't work either - Stas Lelyuk
  • So the problem remains, unfortunately, all the same, in the documentation I found how it can be organized and how you can handle clicks - the problem remains - setNavigationOnClickListener doesn’t want to react to any pressing - abbath0767
  • @ mamba0767 Have you tried clicking on this button in the onOptionsItemSelected method of your Activity? - Stas Lelyuk
  • Naturally. The method works in the case of pressing other elements, but not the Back button, which opens the Drawer. - abbath0767