Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Alexey Markov Avatar asked Mar 18 '16 11:03

Alexey Markov


2 Answers

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.

like image 69
Alexey Markov Avatar answered Sep 19 '22 02:09

Alexey Markov


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);
    }
}
like image 43
Stavkd Avatar answered Sep 22 '22 02:09

Stavkd