Back button using getSupportActionbar and appcompat v7 toolbar

I am using the new toolbar from the Appcompat V7 library, and I am creating an application with a navigation box and fragments.

In some snippets, I don’t want to show the hamburger icon, but the arrow instead ... That's fine, I did it like this:

mDrawerToggle.setDrawerIndicatorEnabled(false); mDrawerToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 

My question is: How or where do I need to configure the lisener home button or what I need to listen to the back button? I want to call the main click method and set the navigation box icon using the hamburger icon.

+50
android appcompat android-toolbar
Dec 01 '14 at 2:42 on
source share
8 answers

You can do it as follows:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); toolbar = (Toolbar)findViewById(R.id.toolbar); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } setUpNavigationDrawer(); getFragmentManager().addOnBackStackChangedListener(backStackListener); // listen to the backstack of the fragment manager } 

Define the onBackSTackChangedListener function:

 private FragmentManager.OnBackStackChangedListener backStackListener = new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { setNavIcon(); }; } 

Set the icon according to your indent fragment:

 protected void setNavIcon() { int backStackEntryCount = getFragmentManager().getBackStackEntryCount(); drawerToggle.setDrawerIndicatorEnabled(backStackEntryCount == 0); } 

Detection when clicking the box icon:

 public boolean onOptionsItemSelected(MenuItem item) { if (drawerToggle.isDrawerIndicatorEnabled() && drawerToggle.onOptionsItemSelected(item)) { return true; } switch (item.getItemId()) { case x: return true; default: return false; } } 

And handle the up button:

 public boolean onSupportNavigateUp() { onBackPressed(); return true; } 

This works for me. Good luck.

+41
Apr 17 '15 at 7:35
source share
β€” -

Add this method to onCreate() :

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

Then override onOptionItemSelected() as shown below:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; default: return super.onOptionsItemSelected(item); } } 
+69
Oct 09 '15 at 14:42
source share

Not sure if this works in the case of OP, but in many cases this is probably the easiest option to implement the back button using the AppCompat toolbar.

Skip all setHomeButtonEnabled , setDisplayHomeAsUpEnabled and onOptionsItemSelected and related issues .

Instead, when initializing the Toolbar, just set 1) the navigation icon and 2) the OnClickListener navigation for it:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (enableBackNavigation) { toolbar.setNavigationIcon(R.drawable.ic_back); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } 
+31
Apr 23 '15 at 13:37
source share

1- Create a Toolbar layout;

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/dark_blue" android:minHeight="?attr/actionBarSize" local:popupTheme="@style/ThemeOverlay.AppCompat.Light" local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

2- Include this in your layout where you want the Toolbar be.

3 Insert the following code into your activity. (extends ActionBarActivity )

 private Toolbar mToolbar; //For Toolbar (Action bar) start mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); mToolbar.setNavigationIcon(R.drawable.ic_back_arrow); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); getSupportActionBar().setTitle("Event Details"); //For Toolbar (Action bar) end 

4 change the back-click icon to whatever you want.

+16
Aug 26 '15 at 6:50
source share

activate the back button:

 getActionBar().setDisplayHomeAsUpEnabled(enable); 

and listening for clicks in onBackPressed()

Obviously your activity should expand ActionBarActivity

+3
Dec 01 '14 at 15:02
source share

You can simply set the navigation icon and make sure that you set setNavigationOnClickListener() after setting setSupportActionBar(toolbar)

 toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back)); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); 
+2
Jan 04 '16 at 8:11
source share

in the manifest add these lines under the action in which you want to work with the back arrow

android: parentActivityName = "Name of parent activity"

+1
Nov 01 '17 at 14:57
source share

Add setDisplayHomeAsUpEnabled (true)

  Toolbar toolbar = findViewById(R.id.toolbar); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); } 

Handle back button

  public boolean onSupportNavigateUp() { onBackPressed(); return true; } 
0
Jan 18 '19 at 3:30
source share



All Articles