12

I have FragmentA and FragmentB and have a problem with setting the title of my Activity when FragmentA becomes back visible.

Flow

  1. FragmentA Visible (not added to the backstack)
  2. add FragmentB (added to the backstack)
  3. back button pressed, not the default implementation but need to capture it in the Fragment but I do getActivity().getSupportFragmentManager().popBackStack();

Now When FragmentA is back visible, the title of the Activity must be changed again like, FragmentA title = "A", FragmentB title = "B". But when FragmentA is back visible, the title is still "B" because onResume isn't called in FragmentA. What are my options to always set title to "A" in FragmentA is visible.

Code:

FragmentA

@Override
public void onResume() {
        super.onResume();
        getActivity().setTitle("POI's");       
}

FragmentB

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        ...
        getActivity().setTitle("POI");
        ...
}
Francesco verheye
  • 1,574
  • 2
  • 14
  • 32
  • 1
    instead of changing Title here you can change Title while replacing Fragment – SilentKiller Sep 03 '14 at 08:45
  • @SilentKiller your comment doesn't describe how to set the title back when FragmentB is gone with pressing the back button. – Francesco verheye Sep 03 '14 at 08:49
  • AFAIK, onResume() should always be called when a fragment/activity becomes visible. Why do you override the Back button functionality? And could you try just for the test to leave the default back button functionality? – dragi Sep 03 '14 at 08:55
  • @Francesco you need to change title at onCreateView only for resume fragment – SilentKiller Sep 03 '14 at 08:56
  • @helleye tried it with the default back button behaviour but without success. onResume of a Fragment is only called when onResume of the Activity is called. But onResume of the Activity isn't called because I don't leave the Activity. – Francesco verheye Sep 03 '14 at 09:00
  • Look this link https://stackoverflow.com/a/46705242/1770868 – Ahmad Aghazadeh Oct 12 '17 at 08:53

2 Answers2

18

I tested on a single Activity with two fragment which worked fine. See the below code.

Fragment A : which show the app name

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle(R.string.app_name);
}

Fragment B : which show the app name

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle("fragment B");
}

Fragment A to B transaction Code :

getActivity().getSupportFragmentManager().beginTransaction()
            .replace(R.id.container,new FragmentB())
            .addToBackStack(null)
            .commit();

Update: Need to replace fragment like "replace(R.id.container,new FragmentB())" rather than add it to FragmentManager to change title of a activity.

Sayem
  • 4,891
  • 3
  • 28
  • 43
4

try to do this

getActivity().getActionBar().setDisplayShowTitleEnabled(true);

getActivity().getActionBar().setTitle("your title");
Mustafa ALMulla
  • 870
  • 2
  • 10
  • 23