title: Unable to change the header and drawer icon and title in toolbar in android
body: <p>I am trying to change the title and drawer icon of toolbar in navigation of one fragment</p>
  <p>Here is what I am trying to implement:</p>
  <p>DrawerItem1 -&gt; HomeFragment -&gt; CategoryFragment -&gt; Frag3 -&gt; Frag4</p>
  <p>DrawerItem2 -&gt; FavoriteFragment</p>
  <p>DrawerItem3 -&gt; Frag6</p>
  <p>DrawerItem4 -&gt; Frag7</p>
  <p>So, here when I navigate from <strong>HomeFragment</strong> to <strong>FavoriteFragment</strong> the toolbar drawer icon changes to back icon and title changes to 'Favorite' as set in navigation. But, when I navigate from <strong>HomeFragment</strong> -&gt; <strong>CategoryFragment</strong> -&gt; <strong>SubCategoryFragment</strong> I am unable to change the toolbar drawer icon to back icon and toolbar title to fragment title.</p>
  <p>Here is my implementation so far.</p>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private AppBarConfiguration mAppBarConfiguration;
  private DrawerLayout drawer;
  private NavController navController;
  private NavHostFragment navHostFragment;
  private NavigationView navigationView;
  public static Toolbar toolbar;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  toolbar = findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  FloatingActionButton fab = findViewById(R.id.fab);
  fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          Snackbar.make(view, &quot;Replace with your own action&quot;, Snackbar.LENGTH_LONG)
                  .setAction(&quot;Action&quot;, null).show();
      }
  });
  drawer = findViewById(R.id.drawer_layout);
  navigationView = findViewById(R.id.nav_view);
  // Passing each menu ID as a set of Ids because each
  // menu should be considered as top level destinations.
  
  navHostFragment =
          (NavHostFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.nav_host_fragment);
  navController = navHostFragment.getNavController();
  mAppBarConfiguration = new AppBarConfiguration.Builder(
          navController.getGraph())
          .setDrawerLayout(drawer)
          .build();
  NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  NavigationUI.setupWithNavController(navigationView, navController);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main_category, menu);
  return true;
  }
  
  @Override
  public boolean onSupportNavigateUp() {
    return NavigationUI.navigateUp(navController, mAppBarConfiguration);
  }
  
  @Override
  public void onBackPressed() {
  
    }
  }
  </code></pre>
  <p><strong>HomeFragment.java</strong></p>
  <pre><code>public class HomeFragment extends Fragment {
  
  private HomeViewModel homeViewModel;
  private RecyclerView rv;
  private ProgressBar pb;
  private DataAdapter adapter;
  private List&lt;DataModel&gt; modelList;
  
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  public View onCreateView(@NonNull LayoutInflater inflater,
                           ViewGroup container, Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_home, container, false);
      rv = v.findViewById(R.id.rv_home);
      pb = v.findViewById(R.id.home_pb);
      return v;
  }
  
  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
  
      modelList = new ArrayList&lt;&gt;();
      Log.d(&quot;Home Frag&quot;, &quot;onViewCreated: called again after back&quot;);
  
      rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
      rv.setHasFixedSize(true);
  
      pb.setVisibility(View.VISIBLE);
      homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
      homeViewModel.getUserMutableLiveData().observe(getViewLifecycleOwner(),
              userListUpdateObserver);
  
      pb.setVisibility(View.GONE);
  
      view.setFocusableInTouchMode(true);
      view.requestFocus();
      view.setOnKeyListener(new View.OnKeyListener() {
          @Override
          public boolean onKey(View v, int keyCode, KeyEvent event) {
              Log.i(&quot;HomeFragment&quot;, &quot;keyCode: &quot; + keyCode);
              if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getAction() == KeyEvent.ACTION_UP) {
                  Log.i(&quot;HomeFragment&quot;, &quot;keyCode: &quot; + keyCode);
                  AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                  builder.setTitle(&quot;Logout&quot;);
                  builder.setMessage(&quot;Are you sure you want to Logout?&quot;);
  
                  // add the buttons
                  builder.setPositiveButton(&quot;Yes&quot;,
                          new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  dialog.dismiss();
                                  getActivity().finish();
                              }
                          });
                  builder.setNegativeButton(&quot;No&quot;, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
                  });
  
                  // create and show the alert dialog
                  AlertDialog dialog = builder.create();
                  dialog.show();
                  return true;
              }
              return false;
          }
      });
  }
  
  Observer&lt;List&lt;Category&gt;&gt; userListUpdateObserver = new Observer&lt;List&lt;Category&gt;&gt;() {
      @Override
      public void onChanged(final List&lt;Category&gt; userArrayList) {
          for (int i = 0; i &lt; userArrayList.size(); i++) {
              modelList.add(new DataModel(userArrayList.get(i).getTitle(), userArrayList.get(i).getImage(), userArrayList.get(i).getId(), 0));
          }
          adapter = new DataAdapter(getContext(), modelList, new DataAdapter.RecyclerViewClickListener() {
              @Override
              public void onClick(View view, int position) {
                  homeViewModel.selectedId(modelList.get(position).getId());
                  homeViewModel.selectedString(modelList.get(position).getText());
                  SubCategoryFragment subCategoryFragment = new SubCategoryFragment();
                  ManageFragments.replaceFragment((FragmentActivity) getContext(), subCategoryFragment);
              }
          });
          rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
          rv.setAdapter(adapter);
          adapter.notifyDataSetChanged();
      }
  };
  }
  </code></pre>
  <p><strong>mobile_navigation.xml</strong></p>
  <pre><code>?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:id=&quot;@+id/mobile_navigation&quot;
  app:startDestination=&quot;@+id/nav_home&quot;&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_home&quot;
      android:name=&quot;com.drawerapp.fragment.home.HomeFragment&quot;
      android:label=&quot;@string/menu_home&quot;
      tools:layout=&quot;@layout/fragment_home&quot;&gt;
  &lt;/fragment&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_sub_category&quot;
      android:name=&quot;com.drawerapp.fragment.sub_category.SubCategoryFragment&quot;
      android:label=&quot;SubCategoryFragment&quot;&gt;
  
      &lt;action
          android:id=&quot;@+id/action_nav_sub_category_to_nav_home&quot;
          app:destination=&quot;@id/nav_home&quot;
          app:popExitAnim=&quot;@anim/nav_default_pop_exit_anim&quot; /&gt;
  &lt;/fragment&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_gallery&quot;
      android:name=&quot;com.drawerapp.fragment.gallery.GalleryFragment&quot;
      android:label=&quot;GalleryFragment&quot;&gt;
      &lt;action
          android:id=&quot;@+id/action_nav_gallery_to_nav_sub_category&quot;
          app:destination=&quot;@id/nav_sub_category&quot;
          app:popExitAnim=&quot;@anim/nav_default_pop_exit_anim&quot; /&gt;
  &lt;/fragment&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_fav&quot;
      android:name=&quot;com.drawerapp.fragment.favorites.FavoritesFragment&quot;
      android:label=&quot;@string/menu_fav&quot;
      tools:layout=&quot;@layout/fragment_favorite&quot;&gt;
      &lt;action
          android:id=&quot;@+id/action_nav_fav_to_nav_home&quot;
          app:destination=&quot;@id/nav_home&quot;
          app:exitAnim=&quot;@anim/fragment_fade_exit&quot;
          app:popExitAnim=&quot;@anim/nav_default_pop_exit_anim&quot; /&gt;
  &lt;/fragment&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_feedback&quot;
      android:name=&quot;com.drawerapp.fragment.feedback.FeedbackFragment&quot;
      android:label=&quot;@string/menu_feedback&quot;
      tools:layout=&quot;@layout/fragment_feedback&quot;&gt;
      &lt;action
          android:id=&quot;@+id/action_nav_feedback_to_nav_fav&quot;
          app:destination=&quot;@id/nav_fav&quot;
          app:exitAnim=&quot;@anim/fragment_fade_exit&quot;
          app:popExitAnim=&quot;@anim/nav_default_pop_exit_anim&quot; /&gt;
  &lt;/fragment&gt;
  
  &lt;fragment
      android:id=&quot;@+id/nav_about_us&quot;
      android:name=&quot;com.drawerapp.fragment.about_us.AboutUsFragment&quot;
      android:label=&quot;@string/menu_about_us&quot;
      tools:layout=&quot;@layout/fragment_about_us&quot;&gt;
      &lt;action
          android:id=&quot;@+id/action_nav_about_us_to_nav_feedback&quot;
          app:destination=&quot;@id/nav_feedback&quot;
          app:exitAnim=&quot;@anim/fragment_fade_exit&quot;
          app:popExitAnim=&quot;@anim/nav_default_pop_exit_anim&quot; /&gt;
  &lt;/fragment&gt;
  </code></pre>