5

I've been researching this topic but so far no luck. Basically I'm replacing a fragment (A) with another one (B) using FragmentTransaction.replace. In this other fragment (B) I have a 'Cancel' button in the toolbar which when pressed pops back to the previous transaction (A) by calling getActivity().getSupportFragmentManager().popBackStackImmediate().

The problem is I need to update the Activity toolbar to display a different title when I'm showing fragment A and fragment B. I can't seem to find a method which gets called in fragment A whenever I go from A -> B -> A to inform me that it is visible again. The idea is to set the toolbar title in this callback which I cannot seem to find.

Can anyone point me in the right direction please?

Cheers.

Edit:

Method I call to replace the fragment with another one is as follows:

public static void replaceFragment(FragmentActivity parentActivity, int fragmentToReplaceId, Fragment withFragment, Integer enterAnim, Integer exitAnim)
{
    FragmentManager         fragmentManager;
    FragmentTransaction     transaction;

    fragmentManager = parentActivity.getSupportFragmentManager();
    transaction     = fragmentManager.beginTransaction();

    if (    (null != enterAnim) &&
            (null != exitAnim)  )
    {
        transaction.setCustomAnimations(enterAnim, exitAnim);
    }

    transaction.replace(fragmentToReplaceId, withFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
Nokiaowner
  • 181
  • 2
  • 11

3 Answers3

4

You can inform by overriding method onResume() in fragment and sending the message to activity or changing the Toolbar directly.

@Override
public void onResume() {
    super.onResume();

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");
}
geNia
  • 985
  • 9
  • 20
  • 1
    It is called in Fragment, not in Activity. – geNia Jan 19 '16 at 21:19
  • Sorry! I meant fragment not activity. In other words when I go from fragment A -> replace fragment A with fragment B -> pop to fragment A, at this point onResume in fragment A does NOT get called. Both fragments are in the same Activity. – Nokiaowner Jan 20 '16 at 08:41
  • @Nokiaowner thats strange, because it should. You should be missing something. – geNia Jan 20 '16 at 16:05
  • As far as I know the fragments onResume() will only be called when the Activities onResume() is called. Keep in mind both fragments are in the same activity. I'm using only one activity during the entire lifetime of the application. – Nokiaowner Jan 20 '16 at 16:20
  • @Nokiaowner the fragment onResume() is called when the fragment is resumed. This can be after the creation of the fragment, or after replacing or in other situations when fragment's state is changed. I have a few applications working in that way, so I am sure. Can you post the code where you manage fragments? – geNia Jan 20 '16 at 16:25
  • I edited my original question showing the code I use to replace the fragment with another one. Thanks for your time! – Nokiaowner Jan 20 '16 at 16:36
  • @Nokiaowner nothing wrong in your code. I have even created a project to test that and I couldn't find a way to replace fragments without calling onResume(). It should be called in your case. Have you tested that on other devices/emulators? – geNia Jan 20 '16 at 16:47
  • Hi geNia. Thanks for the help anyway! I updated my answer below with an alternative solution. Cheers! – Nokiaowner Jan 20 '16 at 18:54
  • 1
    As fragment is tightly bound to Activity. OnResume and onPause will not called when replacing or adding fragment. – Zar E Ahmer Aug 08 '16 at 07:23
1

In one activity, when replace A ---> B (A and B both are fragments), can use this call-back:

 @Override
 public void onAttachFragment(Fragment fragment) {

 }
M.Namjo
  • 374
  • 2
  • 13
-1

Solved by creating a simple static method in fragment A as follows:

public static void updateActivityTitle(FragmentActivity activity)
{
    activity.setTitle(kActivityTitle);
}

Then I'm calling this method in fragment B as follows:

// cancel button has been pressed
private void cancel()
{
    INV_CustomersFragment.updateActivityTitle(getActivity());
    getActivity().getSupportFragmentManager().popBackStackImmediate();
}

Not ideal but it gets the job done. Any other solution involving a proper callback would be better

Update:

A better solution is the one described by @Rani at Fragment's onResume() not called when popped from backstack. This is more elegant and more maintainable, in fact I implemented this solution in my project. Compared to an equivalent solution for iOS this is still messy if you ask me, still seems the way to go.

Community
  • 1
  • 1
Nokiaowner
  • 181
  • 2
  • 11