title: Android Fragment Change Title
body: <p>I have a problem with my fragments. Basically, I have an app which has a navigation drawer and 4 buttons on the activity. When each button is pressed, it will display another fragment. The problem I have is that I want to display the correct title on the <code>ActionBar</code>. I can display the selected item from the navigation on the <code>ActionBar</code>. But when I pressed the button to go to another fragment, I cannot display the title on the <code>ActionBar</code>. Any ideas?</p>
  <p>Here's my sample code:
  <strong>HomeActivity.class</strong></p>
  <pre><code> private void displayView(final int position) {
      // update the main content by replacing fragments
      Fragment fragment = null;
      switch (position) {
          case 0:
              fragment = new HomeFragment();
              break;
          case 1:
              fragment = new MyProfileFragment();
              break;
          case 2:
              fragment = new AboutFragment();
              break;
          default:
              break;
      }
  
      if (fragment != null) {
          fragmentManager = getSupportFragmentManager();
          fragmentManager.beginTransaction()
                  .replace(R.id.frame_container, fragment).commit();
  
          // update selected item and title, then close the drawer
          mDrawerList.setItemChecked(position, true);
          mDrawerList.setSelection(position);
          setTitle(navMenuTitles[position]);
          drawer.closeDrawer(mDrawerList);
      } else {
          // error in creating fragment
          Log.e(&quot;MainActivity&quot;, &quot;Error in creating fragment&quot;);
      }
  
  }
  </code></pre>
  <p>HomeFragment:</p>
  <pre><code>public class HomeFragment extends Fragment {
  
  public HomeFragment() {
  }
  
  CardView card_cart, card_scan, card_categories, card_about;
  Intent i;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.fragment_home, container, false);
      initControls(rootView);
      return rootView;
  }
  
  private void initControls(View rootView) {
  
      card_cart = (CardView) rootView.findViewById(R.id.card_cart);
      card_scan = (CardView) rootView.findViewById(R.id.card_scan);
      card_categories = (CardView) rootView.findViewById(R.id.card_categories);
      card_about = (CardView) rootView.findViewById(R.id.card_about);
  
      card_categories.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              i = new Intent(getActivity(), CategoriesActivity.class);
              startActivity(i);
          }
      });
  
      card_cart.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              MyCartFragment nextFrag = new MyCartFragment();
              getFragmentManager().beginTransaction()
                      .replace(R.id.frame_container, nextFrag)
                      .addToBackStack(null)
                      .commit();
          }
      });
  
      card_about.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              AboutGrabHutFragment aboutFrag = new AboutGrabHutFragment();
              getFragmentManager().beginTransaction()
                      .setBreadCrumbTitle(getResources().getString(R.string.about))
                      .replace(R.id.frame_container, aboutFrag)
                      .addToBackStack(null)
                      .commit();
  
  
          }
      });
  
      card_scan.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              i = new Intent(getActivity(), ScanActivity.class);
              startActivity(i);
          }
      });
  
  }
  </code></pre>
  <p>In the <code>HomeFragment</code> will those 4 buttons be found. On a button click, will go to another fragment. I can display that using this:</p>
  <pre><code>card_about.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              AboutGrabHutFragment aboutFrag = new AboutGrabHutFragment();
              getFragmentManager().beginTransaction()
                      .setBreadCrumbTitle(getResources().getString(R.string.about))
                      .replace(R.id.frame_container, aboutFrag)
                      .addToBackStack(null)
                      .commit();
              getActivity().setTitle(&quot;About&quot;);
  
          }
      });
  </code></pre>
  <p>But the problem is when I go back, the title is still <code>About</code>.</p>