androidandroid-fragmentsandroid-actionbarnavigation-drawerup-button

android - Implement UP button action with Navigation Drawer


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?


Solution

  • 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.