Up navigation does not appear for single action multi-fragment app

I have an Android app that has one main activity that uses a lot of snippets that switch to view mode. I'm not sure if this is the right way to do it, but I inherited this project and would like to avoid doing any major refactorings like changing fragments so that everything is actions or something.

According to Android documentation, it looks like the call to setDisplayHomeAsUp (bool) function should just display the up button by default:

Specify whether the home screen should be displayed as "up". Set this to true if the "home" selection goes back one level in the user interface rather than going back to the top level or first page.

The main problem is that when I use the function:

actionBar.setDisplayHomeAsUpEnabled(true);

      

It doesn't install a button that opens the navigation drawer, instead turns into an Up button. It just removes the "hamburger" ic_drawer from the side. The navigation drawer still opens.

Here is the custom code for NavigationDrawerFragment (copy + paste the exact file you get when you create a new app using the navigation drawer in 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 other fragments that 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 allows me to change the title of the action bar in one separate area and set whether it should have a default back button.

MainActivity.java

public void onSectionAttached (fragment android.app.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);
}

      

Setting the title works fine and there are no errors when calling setDisplayHomeAsUpEnabled (true), but it still doesn't show the Up button. I know right now I am not setting up any fragment hierarchy other than calling addToBackStack (null) in a fragment transaction, but it still seems like this code should be enough for the up button to replace the navigation drawer button.

+3


source to share


2 answers


The problem is that the navigation drawer icon grabs the indicator upwards. In terms of where the View in the action bar displays an icon, the nav drawer icon is also an up icon. This is why you need to call actionBar.setDisplayHomeAsUpEnabled(true);

to display the navigation drawer icon.

To fix this, you need to use ActionBarDrawerToggle#setDrawerIndicatorEnabled(false)

. This will replace the navigation drawer icon with an 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 theme in the android.R.attr.homeAsUpIndicator attribute, instead of the animated glide path of the box.

+3


source


Had the same problem as you and I struggled to get consistent up arrow behavior. I made this example to show you how to do this correctly using one action with a navigation drawer and multiple levels.



https://github.com/tskulbru/android-navdrawer-up-pattern-example

+1


source







All Articles