Search code examples
javaandroidandroid-fragmentsonkeydown

How to handle activity's title with fragments changing


I have a single Activity app with a lot of fragments. How can I change the ActionBar title from activity, even when i press the back button and I return on the previous fragment? My fragments are being displayed on screen with (It's Scala language)

fragmentManager.beginTransaction.add(R.id.content_frame, fragment).addToBackStack(FourMeApp.discussion_backStack).commitAllowingStateLoss()

EDIT: I know how to set title on Activity ActionBar, I am asking about HOW to handle the title when user return to the last fragment with back button.


Solution

  • You can check this link

    As per it you can addOnBackStackChangedListener in your activity and update your title here.

    for HoneyComb and up i.e. Android 3.x and up

    getActionBar().addOnBackStackChangedListener(
     new FragmentManager.OnBackStackChangedListener() {
       public void onBackStackChanged() {
          // Update your UI here.
       }
    });
    

    or for Android 3.x and lower

    getSupportFragmentManager().addOnBackStackChangedListener(
       new FragmentManager.OnBackStackChangedListener() {
         public void onBackStackChanged() {
           // Update your UI here.
         }
     });