6

Question

I am using Navigation Drawer from Android Studio Template. I want to use UP button (arrow) in some of my fragments instend "Hamburger" button. I use AppCompatActivity.

I use this code to show UP button arrow:

public void UseUpButton(boolean value) {
    ActionBar actionBar = getSupportActionBar();
    if (value) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        toggle.setDrawerIndicatorEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
    } else {
        toggle.setDrawerIndicatorEnabled(true);
    }
}

Tried variants:

But I can't catch click on this button. I tried some ways:

Use onOptionsItemSelected

public boolean onOptionsItemSelected(MenuItem item) {

    Log.d("WTF", "menu");
    switch (item.getItemId())
    {
        case android.R.id.home:
            getFragmentManager().popBackStack();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

I add getSupportActionBar().setHomeButtonEnabled(true); to my Activity::onCreate, but onOptionsItemSelected not called when I press the Up button and works correctly when I press the menu items.

Use ActionBarToggle OnClickListener

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,       
            R.string.navigation_drawer_close);

drawer.setDrawerListener(toggle);
toggle.syncState();
toggle.setToolbarNavigationClickListener(new View.OnClickListener()
{
        @Override
        public void onClick(View v) {

        }
 });

But this method doesn't call at Up button press too.

Conclusion:

So, how can I catch the Up button press event?

Alexey Markov
  • 1,546
  • 2
  • 23
  • 41

5 Answers5

7

I found this somewhere few days ago...

In my code I initialize ActionBarDrawerToggle. It has some constructors, but I interested in this:

1

public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
        @StringRes int openDrawerContentDescRes,
        @StringRes int closeDrawerContentDescRes) {
    this(activity, null, drawerLayout, null, openDrawerContentDescRes,
            closeDrawerContentDescRes);
}

2

public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
        Toolbar toolbar, @StringRes int openDrawerContentDescRes,
        @StringRes int closeDrawerContentDescRes) {
    this(activity, toolbar, drawerLayout, null, openDrawerContentDescRes,
            closeDrawerContentDescRes);
}

Take a look: 2nd constructor has Toolbar toolbar argument.

If you want to handle UP BUTTON events DO NOT USE 2nd CONSTUCTOR and use first.

Example:

toggle = new ActionBarDrawerToggle(
            this,
            drawer,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);

getSupportActionBar().setHomeButtonEnabled(true);

setHomeButtonEnabled is important, without this you won't see Hamburger or Up button.

Alexey Markov
  • 1,546
  • 2
  • 23
  • 41
  • 1
    `setHomeButtonEnabled` didnt work for me. But `setDisplayHomeAsUpEnabled` works, also if you want to show the arrow, remember to set `isDrawerIndicatorEnabled` to false – Francisco Durdin Garcia Jan 06 '18 at 17:28
3

I added a few lines to your method so it would implement the back button:

  public void useUpButton(boolean value) {
    ActionBar actionBar = getSupportActionBar();
    if (value) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        toggle.setDrawerIndicatorEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    } else {
        toggle.setDrawerIndicatorEnabled(true);
        toggle.setToolbarNavigationClickListener(null);
    }
}
Stavkd
  • 31
  • 5
1

For the default behavior which makes the hamburger button open drawer you can just use this code

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mActionBarDrawerToggle = new ActionBarDrawerToggle(
            this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(mActionBarDrawerToggle);
    mActionBarDrawerToggle.syncState();

then to override the default behaviour in any of the child activity, do this

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);  // shows the up navigation button instead of the hamburger

    mToolbar.setNavigationOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            finish(); //end current activity
        }
    });

When ActionBarDrawerToggle takes toolbar as input, it sets its own NavigationOnClickListener on toolbar. we can override this to call our code when up button is clicked.

Abhishek Agrawal
  • 2,183
  • 1
  • 17
  • 24
0

Hi If you want to intercept the action bar items click from a fragment you need at first add this to your fragment onCreate:

setHasOptionsMenu(true);

Than just use the right method onOptionsItemSelected (fragment version different when you use in an activity):

@Override public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.your_action:
                //do something
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }//onOptionsItemSelected
br00
  • 1,391
  • 11
  • 21
0

Hope It will Help You...

DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);//for fragment
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        //do samething
    }
}

or use this

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);//for fragment
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        finish();
    }
}
Nandkishor mewara
  • 2,552
  • 16
  • 29
  • 1
    This code is exists in Navigation Drawer template. I implement here pop fragments from stack, but it only work on hardware back button, not up button in application bar – Alexey Markov Mar 18 '16 at 12:41