Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling setDisplayHomeAsUpEnabled for fragments in ActionBarCompat

I am using ActionBarCompat. When I load a child fragment, I want the home button to work as the up button. So I called this from the child fragment:

((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

But still the home button is not being shown as the up button. I have also added logic for the id android.R.id.home in onOptionsItemSelected, but it still does not work. Any ideas how I can get it done?

like image 763
Raquib-ul Alam Avatar asked Feb 02 '14 17:02

Raquib-ul Alam


3 Answers

class ABC : Fragment()  {
    override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? { {

        (activity as MainActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false)
    }
}
like image 69
gavrbhat Avatar answered Nov 19 '22 17:11

gavrbhat


I've struggled with this for a couple of long days, and here's what I've found to work. I'm hoping there's a better solution, but this does get the job done:

In my main activity (the one launching the fragment), create the following public function, which will be called by the "child" fragment:

// The method is in MainActivity.java
public void resetActionBar(boolean childAction, int drawerMode)
{
    if (childAction) {
        // [Undocumented?] trick to get up button icon to show
        drawerToggle.setDrawerIndicatorEnabled(false);
        mActionBar.setDisplayHomeAsUpEnabled(true);
    } else {
        drawerToggle.setDrawerIndicatorEnabled(true);
    }

    drawerLayout.setDrawerLockMode(drawerMode);
}

Then, from your fragment that you want the Up button to appear, just call that method as follows (adapting class names as needed):

// This method in in SomeFragment.java
((MainActivity)getActivity()).resetActionBar(true,
      DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

To summarize, here's how to enable up button from fragment:

  • Disable the drawer indicator on the drawerToggle object -- call setDrawerIndicatorEnabled(false)
  • Set displayHomeAsUp -- call setDisplayHomeAsUpEnable(true) on actionBar object
  • Optionally, lock the drawer, so it won't appear on edge swipe

Hope this helps, and I'm hoping this becomes easier in the future...

like image 10
rodrigo-silveira Avatar answered Nov 19 '22 18:11

rodrigo-silveira


Bit late to the party:)

I'm sharing this so that it might help someone. I have head around lots of solutions and none of them works perfectly. I've used variation of solutions available in my project which is here as below. Please use this code inside class where you are initialising toolbar and drawer layout.

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(false);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);// show back button
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
            } else {
                //show hamburger
                drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(true);
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                drawerFragment.mDrawerToggle.syncState();
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        drawerFragment.mDrawerLayout.openDrawer(GravityCompat.START);
                    }
                });
            }
        }
    });
like image 1
Geek Avatar answered Nov 19 '22 17:11

Geek