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);
}
}
But I can't catch click on this button. I tried some ways:
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.
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.
So, how can I catch the Up button press event?
I found this somewhere few days ago...
In my code I initialize ActionBarDrawerToggle
. It has some constructors, but I interested in this:
public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
@StringRes int openDrawerContentDescRes,
@StringRes int closeDrawerContentDescRes) {
this(activity, null, drawerLayout, null, openDrawerContentDescRes,
closeDrawerContentDescRes);
}
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With