ActionBarDrawerToggle disappears when addToBackStack(null) is used

81 Views Asked by At

In the selectItem() method, adding .addToBackStack(null) to my transaction seems to break the toggle button (the one that brings out the nav drawer when tapped). It just disappears entirely when a fragment is loaded (by clicking on the corresponding item in the nav drawer). If I comment out that line, the toggle button remains and works perfectly fine.

I need to call addToBackStack because I want back button functionality to work for those fragments, though. Why is it causing this issue?

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Log.d(TAG, "Item " + position + " selected");
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment aboutFragment = new AboutFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    Log.d(TAG, "selectItem() with position  " + position);

    switch(position) {
        case 0:
            fragmentManager.beginTransaction()
                    .replace(R.id.contributionsFragmentContainer, contributionsList)
                    //.addToBackStack(null)
                    .commit();
            Log.d(TAG, "contributionsListFragment committed");
            break;
        case 1:
            fragmentManager.beginTransaction()
                    .replace(R.id.contributionsFragmentContainer, aboutFragment)
                    //.addToBackStack(null)
                    .commit();
            Log.d(TAG, "aboutFragment committed");
            break;

    }

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(itemArray[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    if (title != null && getActionBar() != null) {
        getActionBar().setTitle(title);
    }
}

private void addDrawerItems() {
    itemArray = new String[] {"Home", "About", "Nearby", "Settings", "Feedback"};
    Log.d(TAG, "Adding to itemArray");
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.title_activity_contributions);
    setContentView(R.layout.activity_contributions);
    Log.d("ContributionsActivity", "onCreate()");

    // Activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById()
    contributionsList = (ContributionsListFragment) getSupportFragmentManager().findFragmentById(R.id.contributionsListFragment);

    //Set up navigation drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.drawer_list);
    addDrawerItems();
    setupDrawer();
    ...
}
0

There are 0 best solutions below