Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTitle when Fragment is visible again

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");
        ...
}
like image 647
Francesco verheye Avatar asked Sep 03 '14 08:09

Francesco verheye


1 Answers

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.

like image 103
Sayem Avatar answered Oct 21 '22 02:10

Sayem