In my application ( minSdkVersion 15) I have Toolbar instead of ActionBar and NavigationDrawer switching between fragments. Some fragments have a TabBar with child fragments inside. These are child fragments of ListView s, and their onItemClickListener triggers DetailFragment . I call setDisplayHomeAsUpEnabled() , and an up arrow appears for the DetailFragment , but I cannot process it to perform any action, or even show a toast. I tried this and processed switch (item.getItemId()) in onOptionsItemSelected() , but no solution works for me. I’m missing something, but I can’t understand.
Here is my code:
public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {
...
public void shouldDisplayHomeUp(){ //Enable Up button only if there are entries in the back stack boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0; if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(canback); actionBarDrawerToggle.setDrawerIndicatorEnabled(!canback); } } @Override public boolean onNavigateUp() { //This method is called when the up button is pressed. Just the pop back stack. getSupportFragmentManager().popBackStack(); return true; } @Override public void onBackStackChanged() { shouldDisplayHomeUp(); }
All my onOptionsItemSelected :
@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. switch (item.getItemId()) { case android.R.id.home: Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show(); //called when the up affordance/carat in actionbar is pressed onBackPressed(); break; case R.id.action_search: Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
I don't know if this other code makes sense:
// Initializing Drawer Layout and ActionBarToggle mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.openDrawer, R.string.closeDrawer){//... //Setting the actionbarToggle to drawer layout mDrawerLayout.setDrawerListener(actionBarDrawerToggle); //calling sync state is necessay or else your hamburger icon wont show up actionBarDrawerToggle.syncState();
In my manifest, I have no parenting activity.
I found this one , and this seems to be the correct answer, but I don’t understand how to implement it, and I don’t have a reputation to ask the author.
What can I do to make the up button work?
Two new questions: when did I install
public void shouldDisplayHomeUp(){ //Enable Up button only if there are entries in the back stack if (getSupportActionBar() != null) { if (getSupportFragmentManager().getBackStackEntryCount()>0) { actionBarDrawerToggle.setDrawerIndicatorEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { getSupportActionBar().setDisplayHomeAsUpEnabled(false); actionBarDrawerToggle.setDrawerIndicatorEnabled(true); } } }
... and from the child snippet I press the back button of the hardware. I go back to the previous snippet and the hamburger icon appears. But the up button is still not responding from the child fragment. I believe because ActionBarDrawerToggle no longer controls its behavior. But who controls it? If I install this method as follows:
public void shouldDisplayHomeUp(){ //Enable Up button only if there are entries in the back stack if (getSupportActionBar() != null) { if (getSupportFragmentManager().getBackStackEntryCount()>0) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { getSupportActionBar().setDisplayHomeAsUpEnabled(false); } } }
... then ActionBarDrawerToggle handles a click on the up button and opens the box, like the hamburger icon. But when I press the hardware return button, the up button (arrow) disappears and the hamburger icon does not appear.
So now I see two ways to solve this problem. First, I could find out who controls the up button when
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
and
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
or else, how can I override the behavior of an ActionBarDrawerToggle to perform another action when the up button arrow is available instead of the hamburger icon? And how to call the hamburger icon when I press the back button?