Control the navigation bar and the back button of the toolbar from fragment to fragment in android

All my fragments are controlled using ActionBarActivity ( mainActivity ), inside DrawerLayout implemented by DrawerLayout , and all child fragments are pushed through the drawerLayout list item, click the button. The problem I encountered is that after clicking a fragment through drawerLayout I want to change the box icon to the ToolBar back icon so that the user can go to the previous fragment and handle the android.R.id.home callback or inside the same fragment or inside mainActivity .

+5
source share
1 answer

You have an extra Fragment back back and add a listener to the fragment manager, for example

 getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); // show back button toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } else { //show hamburger getSupportActionBar().setDisplayHomeAsUpEnabled(false); toggle.syncState(); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(GravityCompat.START); } }); } } }); 
+11
source

Source: https://habr.com/ru/post/1247945/


All Articles