Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Back button" using getSupportActionbar and appcompat v7 toolbar

I'm using the new toolbar from the Appcompat V7 library and I'm making an application with navigation drawer and with fragments.

In some fragments I don't want to show the hamburger icon but the arrow instead... That is fine I did this in this way:

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 that: How or where i need to set up the home button lisener or what i need to listen for the "back" button ? I want to call the main backpressed method and to set back the navigation drawer icon with the hamburger icon..

like image 496
András Ferencz Avatar asked Dec 01 '14 14:12

András Ferencz


People also ask

How do I get the back button on my toolbar?

Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.

What is Androidx Appcompat widget toolbar?

androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.


2 Answers

Add this method in onCreate():

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

Then override the onOptionItemSelected() as below:

@Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {         case android.R.id.home:             onBackPressed();             return true;         default:             return super.onOptionsItemSelected(item);     } } 
like image 131
Raja Jawahar Avatar answered Oct 14 '22 07:10

Raja Jawahar


You can do it like this:

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:

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

Set the icon according to your fragment's backstack:

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

Detect when the drawer icon is pressed:

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.

like image 35
tanneebee Avatar answered Oct 14 '22 09:10

tanneebee