I am using navigation drawer with fragments and in my Home page i am navigating like this==> maincategory===>subcategory===>subcategoryDetail

while moving from maincategory to subcategory it perfectly replaces the hamburger icon and shows back arrow icon and onback press is working perfectly

but while moving from subcategory to subcategoryDetail when i press the back arrow of subcategoryDetail onback press is working perfectly but it replaces the back arrow of subcategory.java page with hamburger icon and i cannot go back to home page

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    Toolbar toolbar;
    DrawerLayout drawer;
    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        displaySelectedScreen(R.id.nav_home);

    }


    public ActionBarDrawerToggle getToggle() {
        return toggle;
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            int fragments = getSupportFragmentManager().getBackStackEntryCount();
            if (fragments == 1) {
                finish();
            } else if (getFragmentManager().getBackStackEntryCount() > 1) {
                getFragmentManager().popBackStack();

            } else {
                super.onBackPressed();
                toggle.setDrawerIndicatorEnabled(true);
            }
        }
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        displaySelectedScreen(item.getItemId());

        return true;
    }

    private void displaySelectedScreen(int itemId) {
        Bundle bundle = new Bundle();
        //creating fragment object

        Fragment fragment = null;
        //initializing the fragment object which is selected
        switch (itemId) {
            case R.id.nav_home:
                fragment = new HomeFragment();
                break;
            case R.id.nav_mens_clothing:
                bundle.putString("MAINCATEGORYID", String.valueOf(1));
                bundle.putString("MAINCATEGORYNAME","Carpentry");
                fragment = new SubCategoryFragment();
                fragment.setArguments(bundle);
                break;
            case R.id.nav_womens_clothing:
                bundle.putString("MAINCATEGORYID", String.valueOf(2));
                bundle.putString("MAINCATEGORYNAME","Electrical");
                fragment = new SubCategoryFragment();
                fragment.setArguments(bundle);
                break;
            case R.id.nav_boys_clothing:
                fragment = new SubCategoryFragment();
                break;
        }

        //replacing the fragment
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    }

}

SubCategoryFragment.java

public class SubCategoryFragment extends Fragment {
    protected Toolbar toolbar;
    protected ActionBarDrawerToggle toggle;

    View view;

    public SubCategoryFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_sub_category, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();
            shimmerContainer = view.findViewById(R.id.shimmer_view_container);
            recyclerView_subcategory = view.findViewById(R.id.recycler_view_category);

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });



        }
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        toolbar.setTitle("Cooper");
        toggle.setDrawerIndicatorEnabled(true);

    }

SubCategoryDetailFragment.java

public class SubCategoryDetailFragment extends Fragment {
    protected Toolbar toolbar;
    protected ActionBarDrawerToggle toggle;
    View view;

    public SubCategoryDetailFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_sub_category_detail, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });

        }
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        toolbar.setTitle("zzz");
        toggle.setDrawerIndicatorEnabled(true);

    }}
}

2 Answers

0
Venky On

Design two different toolbars in XML and set their visibility according to fragment and activity. When Activity load set Toolbar with Navigation drawer, but when fragment load in that activity hide the current toolbar and replace with another toolbar XML with the back icon.

Do not forget to add toolbar code in onResume() method of the fragment.

0
Developer On

Please add these two lines in onresume it will work.

 toggle.setDrawerIndicatorEnabled(false);
 toggle.setHomeAsUpIndicator(R.drawable.back);