Issue
I have an android application that has a single main activity that employs many fragments that switch into view. I'm not sure if that's the right way to do it, but I have inherited this project and would like to avoid doing any major refactors like changing the fragments to all be activities or something like that.
According to the android documentation, it looks like calling the setDisplayHomeAsUp(bool) function should just display the up button by default:
Set whether home should be displayed as an "up" affordance. Set this to true if selecting "home" returns up by a single level in your UI rather than back to the top level or front page.
The main issue is that when I use the function:
actionBar.setDisplayHomeAsUpEnabled(true);
It does not set the button that opens the navigation drawer to instead turn into an 'Up' button. It just removes the 'hamburger' ic_drawer icon from the side. The navigation drawer still opens.
Here is the custom code for the NavigationDrawerFragment (I copy+pasted the exact file that you get when you create a new application with a navigation drawer within android studio):
NavigationDrawerFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); mDrawerListView = (ListView) inflater.inflate( R.layout.fragment_navigation_drawer, container, false); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } }); PopulateAppDrawerList(); return mDrawerListView; } public void PopulateAppDrawerList() { List<AppOption> allApps = getAllApps(); List<AppOption> filteredApps = new ArrayList<AppOption>(); for (int i = 0; i < allApps.size(); i++) { if (allApps.get(i).getLaunchable()) { filteredApps.add(allApps.get(i)); } } NavDrawerListAdapter adapter = new NavDrawerListAdapter(filteredApps, MainActivity.getInstance()); mDrawerListView.setAdapter(adapter); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); }
Then, I have the other fragments that all extend 'BaseAppFragment', which contains the following:
BaseAppFragment.java
public class BaseAppFragment extends Fragment {
@Override public void onStart() { super.onStart(); MainActivity.getInstance().onSectionAttached(this); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } }
This is what allows me to change the title on the action bar in one single area and set whether or not it should have the back button set by default.
MainActivity.java
public void onSectionAttached(android.app.Fragment fragment) { Class fragmentType = fragment.getClass();
ActionBar actionBar = getActionBar(); mNavigationDrawerFragment.PopulateAppDrawerList(); if (fragmentType != null) { if (fragmentType.equals(AuthenticationFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(MyOptionsFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "My Options"; } else if (fragmentType.equals(GLAuthenticationFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(InitialLoginFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(LoginFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(DailyOverviewFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); mTitle = "Overview"; } else if (fragmentType.equals(SingleComponentFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); SingleComponentFragment singleComponentFragment = (SingleComponentFragment) fragment; if (singleComponentFragment != null && singleComponentFragment .mComponent != null) { mTitle = String.format("Back To Day %s", singleComponentFragment.mComponent.getDay() + ""); } else { mTitle = ""; } } else if (fragmentType.equals(singleDayOverviewFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); mTitle = "Back To Overview"; } } actionBar.setTitle(mTitle); }
The title setting works perfectly and there are no errors when the setDisplayHomeAsUpEnabled(true) is called, but it still shows no Up button. I know that right now I am not setting any type of fragment navigation hierarchy other than the addToBackStack(null) call in the Fragment Transaction, but it still seems like this code should be enough to have the up button replace the navigation drawer button.
Solution
The problem is that the navigation drawer icon hijacks the up indicator. In terms of which View in the action bar is displaying the icon, the navigation drawer icon is also the up icon. This is why you need to call actionBar.setDisplayHomeAsUpEnabled(true); for the navigation drawer icon to show.
To fix this, you need to use ActionBarDrawerToggle#setDrawerIndicatorEnabled(false). This will replace the navigation drawer icon with the up icon. From the documentation for this method:
When the indicator is disabled, the ActionBar will revert to displaying the home-as-up indicator provided by the Activity's theme in the android.R.attr.homeAsUpIndicator attribute instead of the animated drawer glyph.
Answered By - Bryan Herbst
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.