Android Club – Change ActionBarDrawerToggle for fragments: hamburger/back icon

This is not tutorial. This is example code how to implement one fragment as Home Fragment, others as children. If you are on home parent fragment – app shows hamburger icon, if you are on child fragment, it will show back button. If you want to know more about how to implement Material Design Navigation Drawer, you can open this post here

Download Android Studio Project: AndroidDrawerToolbarFragmentTutorial

If you just want to see code, here it is:

First of all, download following icons and copy to res/drawables folder:

ic_arrow_back

menu_icon1

menu_icon2

menu_icon3

menu_icon4

menu_icon5

MainActivity.java

public class MainActivity extends AppCompatActivity {

    static FragmentManager manager;
    static Context context;
    public static AppCompatActivity activity;
    public static Drawer drawer;
    public static ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();
        activity = MainActivity.this;
        manager = getSupportFragmentManager();

        showToolbar();
        showNavigationDrawer();
        toggle = new ActionBarDrawerToggle(this, drawer.getDrawerLayout(), R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        MainActivity.activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        MainActivity.toggle.syncState();

        changeFragment(new HomeFragment(), true);
    }

    private void showToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }


    private void showNavigationDrawer() {
        DrawerBuilder builder = new DrawerBuilder()
                .withActivity(this)
                .addDrawerItems(
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon2).withName(R.string.drawer_title2),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon3).withName(R.string.drawer_title3),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon4).withName(R.string.drawer_title4),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon5).withName(R.string.drawer_title5)
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {

                        switch (position) {
                            case 0:
                                changeFragment(new AnotherFragment(), true);
                                break;
                            case 1:
                                changeFragment(new TabbedFragment(), true);
                                break;
                            case 2:
                                startActivity(new Intent(MainActivity.this, ActivityTwo.class));
                                break;
                            case 3:
                                finish();
                                break;
                        }

                        return false;

                    }
                });

        drawer = builder.build();
    }

    public static void changeFragment(Fragment fragment, boolean doAddToBackStack) {

        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.flFragments, fragment);
        if (doAddToBackStack) {
            transaction.addToBackStack(null);
            activity.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back);
        } else {
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toggle.syncState();
        }
        transaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onBackPressed() {
        Fragment fragment = manager.findFragmentById(R.id.flFragments);
        String fragmentName = fragment.getClass().getSimpleName();
        if (fragmentName.equals("HomeFragment")) {
            drawer.openDrawer();
        } else {
            super.onBackPressed();
        }

    }


}

HomeFragment

public class HomeFragment extends Fragment {
   
    public HomeFragment() {
        // Required empty public constructor
    }
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        MainActivity.activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        MainActivity.toggle.syncState();

        Button bChildFragment = (Button) view.findViewById(R.id.bChildFragment);
        bChildFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.changeFragment(new ChildFragment(), true);
            }
        });

        return view;
    }
   
}