title: How to set title in app bar with Navigation Architecture Component
body: <p>I was trying out Navigation architecture component and is now having difficulties in setting the title. How do I set the title programmatically and also how it works?</p>
  
  <p>To clear my question, let's have an example, where, I've set up a simple app with <code>MainActivity</code> hosting the navigation host controller, the <code>MainFragment</code> has a button and on clicking the button it goes to <code>DetailFragment</code>.</p>
  
  <p>The same code from another question of <a href="https://stackoverflow.com/questions/52483927/two-different-menus-for-top-app-bar-and-bottom-app-bar-with-navigation-component">multiple app bars</a> on stack-overflow.</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          // Setting up a back button
          NavController navController = Navigation.findNavController(this, R.id.nav_host);
          NavigationUI.setupActionBarWithNavController(this, navController);
      }
  
      @Override
      public boolean onSupportNavigateUp() {
          return Navigation.findNavController(this, R.id.nav_host).navigateUp();
      }
  }
  </code></pre>
  
  <p><strong>MainFragment</strong></p>
  
  <pre><code>public class MainFragment extends Fragment {
  
      public MainFragment() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_main, container, false);
      }
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          Button buttonOne = view.findViewById(R.id.button_one);
          buttonOne.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.detailFragment));
      }
  
  }
  </code></pre>
  
  <p><strong>DetailFragment</strong></p>
  
  <pre><code>public class DetailFragment extends Fragment {
  
      public DetailFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_detail, container, false);
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:animateLayoutChanges="true"
      tools:context=".MainActivity"&gt;
  
      &lt;com.google.android.material.appbar.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:animateLayoutChanges="true"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;androidx.appcompat.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
      &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
      &lt;fragment
          android:id="@+id/nav_host"
          android:name="androidx.navigation.fragment.NavHostFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="top"
          android:layout_marginTop="?android:attr/actionBarSize"
          app:defaultNavHost="true"
          app:layout_anchor="@id/bottom_appbar"
          app:layout_anchorGravity="top"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          app:navGraph="@navigation/mobile_navigation" /&gt;
  
      &lt;com.google.android.material.bottomappbar.BottomAppBar
          android:id="@+id/bottom_appbar"
          android:layout_width="match_parent"
          android:layout_height="?android:attr/actionBarSize"
          android:layout_gravity="bottom" /&gt;
  
      &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_anchor="@id/bottom_appbar" /&gt;
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p><strong>navigation.xml</strong></p>
  
  <pre><code>&lt;navigation xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/mobile_navigation"
      app:startDestination="@id/mainFragment"&gt;
      &lt;fragment
          android:id="@+id/mainFragment"
          android:name="com.example.MainFragment"
          android:label="fragment_main"
          tools:layout="@layout/fragment_main" &gt;
          &lt;action
              android:id="@+id/toAccountFragment"
              app:destination="@id/detailFragment" /&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id="@+id/detailFragment"
          android:name="com.example.DetailFragment"
          android:label="fragment_account"
          tools:layout="@layout/fragment_detail" /&gt;
  &lt;/navigation&gt;
  </code></pre>
  
  <p>So when start my app, <strong>the title is "MainActivity"</strong>. As usual it shows the <code>MainFragment</code> that contains the button to go to <code>DetailFragment</code>. In the <code>DialogFragment</code> I've set the title as:</p>
  
  <pre><code>getActivity().getSupportActionBar().setTitle("Detail");
  </code></pre>
  
  <p><strong>First Problem:</strong> So clicking the button on the <code>MainFragment</code> to goto <code>DetailFragment</code>, it does go there and the title changes to "Detail". But on clicking the back button, the <strong>title changes to "fragment_main"</strong>. So I added this line of code to <code>MainFragment</code>:</p>
  
  <pre><code>@Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      // ...
  
      //Showing the title 
      Navigation.findNavController(view)
        .getCurrentDestination().setLabel("Hello");
  }
  </code></pre>
  
  <p>Now the while returning back from <code>DetailFragment</code> to <code>MainFragment</code> the title changes to "Hello". But here comes the <strong>second problem</strong>, when I close the app and start again, the title changes back to "MainActivity" though it should be showing "Hello" instead, know?</p>
  
  <p>Ok, then adding <code>setTitle("Hello")</code> in <code>MainFrgment</code> is not working too. For example, the activity starts and the title is "Hello", go to <code>DetailsFragment</code> and press the back button again, the title goes back to "fragment_main".</p>
  
  <p>The only solution is to have both <code>setTitle("Hello")</code> along with <code>Navigation.findNavController(view).getCurrentDestination().setLabel("Hello")</code> in <code>MainFragment</code>.</p>
  
  <p>So what is the proper way to show the title for fragments using Navigation Component?</p>