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?
class ABC : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? { {
(activity as MainActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false)
}
}
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:
Hope this helps, and I'm hoping this becomes easier in the future...
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);
}
});
}
}
});
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