3

I have FragmentA and FragmentB. I added FragmentA to an Activity using the add (not replace) fragment transaction. A button in FragmentA makes a callback to it's Activity which then makes an add fragment transaction to add FragmentB.

I update the title of the ActionBar with the value of a variable (which is fetched online) in FragmentA and FragmentB.

After the fragment transactions I outlined above, if I'm in FragmentB and I press the back button (now I'm in FragmentA), the title of the ActionBar is still that of FragmentB.

So I wanted to know which lifecycle methods are called on FragmentA when I'm coming from FragmentB so I can update the Actionbar from there.

Roseyk
  • 97
  • 9
  • You should do the setting of the title in `onCreateView` of the respective `Fragment`. So in your `FragmentA`, set title of the `ActionBar` to whatever you desire, and do the same in `FragmentB`'s `onCreateView` method. Let me know if this gives the behavior you desire. – ishmaelMakitla May 29 '16 at 16:45
  • It won't work. `onCreatView` won't be called. Check here http://stackoverflow.com/q/18634207/6215423 – Roseyk May 29 '16 at 17:21
  • @Roseyk try with onViewCreated method..... – Muhammad Waleed May 29 '16 at 19:00

2 Answers2

0

I don't know if there is a method that is called when backstack is popped, but what you can do is overwrite onBackPressed in your Activity class that contains the fragments, call fragmentManager.popBackStackImmediate. This method returns a boolean, if it's true, then you went from fragmentB to fragmentA (or to any other previous fragment). You are in your activity class, so you can update your ActionBar anyway you'd like. One more thing, if fragmentManager.popBackStackImmediate is true, don't call super.onBackPressed()!!!

Hampel Előd
  • 405
  • 2
  • 8
  • 19
-2

onStart() method is clearly the adapted one. When comming from backstack, it will always be called.

Pdroid
  • 867
  • 9
  • 13