Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Toolbar to drawer/menu icon after popBackStack()?

I have an Android application with a MainActivity that swaps a content area with fragments upon presses of buttons. With the current code (found below), it can successfully

  1. swap the content area with a new fragment
  2. display the "Up navigation" (back button) in the Toolbar
  3. navigate "up"/"back" (restore previous state of the content area)

... But the Toolbar itself doesn't update to display the "drawer"/"menu" (hamburger) button when it gets back to its original state (getSupportFragmentManager().getBackStackEntryCount() == 0). I imagine I need to implement FragmentManager.OnBackStackChangedListener on my activity, though my attempts haven't been so successful. I've also seen mentions of syncState(), though I haven't had too much luck there either.

How do I return the Toolbar home button back to the "drawer"/"menu" appearance and functionality after swapping and popping fragments?

My code can be found below. Tried to simplify it a bit before sharing, hopefully I didn't cut anything important.

MainActivity.java:

package me.stashhh.stashboxes;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import me.stashhh.stashboxes.model.CountryManager;


public class MainActivity extends ActionBarActivity {
    private Toolbar toolbar;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // normal onCreate stuff
        super.onCreate(savedInstanceState);

        // The ContentView that we're referencing
        setContentView(R.layout.activity_main);

        // Toolbar setup
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        // Drawer setup
        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.action_drawer_open,
                R.string.action_drawer_close
        );
        mDrawerLayout.setDrawerListener(mDrawerToggle); // REQUIRED FOR ARROW ANIMATION. Fml.
        mDrawerToggle.syncState();


        // Listen for changes in the back stack
        getSupportFragmentManager().addOnBackStackChangedListener(this);
    }

    public void onBackPressed() {
        // Catch back action and pops from backstack
        // (if you called previously to addToBackStack() in your transaction)
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        } else {
            // Default action on back pressed
            super.onBackPressed();
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Get item selected and deal with it
        switch (item.getItemId()) {
            case android.R.id.home:
                //called when "up" (back button) is called in toolbar
                onBackPressed();
                return true;
        }
        return true; // default return since 'boolean'; not sure if proper?
    }



    /**
     * Called when the user clicks the "View Stashboxes" button
     */
    public void viewStashboxes(View view) {
        // update the main content by replacing fragments
        Fragment stashboxesFragment = new StashboxesFragment();

        Bundle args = new Bundle();
        args.putString(StashboxesFragment.ARG_TEST_STRING, "Testing 1.. 2.. 3..");
        stashboxesFragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();

        // Add this transaction to the back stack...
        // http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, stashboxesFragment, "stashboxesFragment");
        fragmentTransaction.addToBackStack("stashboxesFragment");
        fragmentTransaction.commit();

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // If drawer is open, we have to close it...
            DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); // The drawer layout
            LinearLayout mDrawerList = (LinearLayout) findViewById(R.id.left_drawer); // The drawer "list" itself
            mDrawerLayout.closeDrawer(mDrawerList); // Close the drawer list
        }
    }

    /**
     * Fragment that appears in the "content_frame"
     */
    public static class StashboxesFragment extends Fragment {
        public StashboxesFragment() {
            // empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // REDACTED CODE FOR SIMPLICITY.
            // inflate a layout in the main content area.

            return rootView;
        }
    }
}

I'm super new to Android development, so any other critiques you might have would also be appreciated. Thank you.

like image 478
Tyler Nieman Avatar asked Oct 31 '22 13:10

Tyler Nieman


1 Answers

This works for me : Please update your onBackPressed()

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub      
    if(frgManager.getBackStackEntryCount()>0){      
           getSupportFragmentManager().popBackStack();      
           // shows the drawer icon when on moving back 
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setHomeButtonEnabled(false);     
        }else{
            super.onBackPressed();
        }
    mDrawerToggle.syncState();
}
like image 65
Dory Avatar answered Nov 13 '22 04:11

Dory