Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback when Fragment is replaced and when it is visible again

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();
}
like image 949
Nokiaowner Avatar asked Sep 26 '22 15:09

Nokiaowner


2 Answers

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");
}
like image 109
geNia Avatar answered Oct 08 '22 13:10

geNia


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

 @Override
 public void onAttachFragment(Fragment fragment) {

 }
like image 1
M.Namjo Avatar answered Oct 08 '22 13:10

M.Namjo