UpButton not working, what I'm trying to do

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 { //... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Listen for changes in the back stack getSupportFragmentManager().addOnBackStackChangedListener(this); //Handle when activity is recreated like on orientation Change shouldDisplayHomeUp(); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); final ActionBar actionBar = getSupportActionBar(); //... 

...

 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?

+5
source share
2 answers

The answer is simple, as I expected, but I spend more than one day to figure this out. I followed step by step, opening the documentation for each method. Ran application to notice any changes. I realized that when setDisplayHomeAsUpEnabled (true), it changes the hamburger icon of the box to the back arrow icon, but when you press the back arrow, it performs the same action (show-hide the box).

If you want to implement a different behavior for the back arrow, you must set setDrawerIndicatorEnabled (false) and then setToolbarNavigationClickListener (). So

 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); // order matters getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { getSupportActionBar().setDisplayHomeAsUpEnabled(false); actionBarDrawerToggle.setDrawerIndicatorEnabled(true); } } 

and

 actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "ToolbarNavigationClickListener", Toast.LENGTH_SHORT).show(); } }); 

work together to implement UpButton custom behavior. Hope this helps someone.

+3
source

It seems that you did not handle the ActionBarDrawerToggle inside onOptionsItemSelected() . Also return true to use the selection. Try the following:

 @Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle your other action bar items... 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(); return true; case R.id.action_search: Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } 
0
source

All Articles