1

I have an activity with 2 fragments and drawer. So when i am in fragment i get "Up" icon instead hamburger icon (with the help of setDisplayHomeAsUpEnabled) but the action is still the same - navigation_drawer_open/close. So how to get onBackPressed() instead?

And according to this comment i dont know how to handle the Home/Up by myself because of "automatically handle clicks".

 @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.
SERG
  • 3,907
  • 8
  • 44
  • 89

2 Answers2

0

Automatic handling usually only works between activities. Since you are using fragments you may need to handle them manually.

Here's an example on how to handle a back button press.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: // This is the home/back button
                onBackPressed(); // Handle what to do on home/back press
                break;
        }

        return false;
    }
Tarps
  • 1,928
  • 1
  • 11
  • 27
  • I did try this, but in my case looks like onOptionsItemSelected did not fired at all if i click on home\upp button – SERG Nov 12 '15 at 17:27
0

For do this you must use Custom Toolbar and instead of android menu key use from this library :
material-menu


In this library you can change state for arrow and rotate -90 for show Up Icon instead of Burger icon.

Soheil Tayyeb
  • 315
  • 4
  • 13