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?