-3

I have an app with several Fragments which interact to each other. Sometimes I have to add a Fragment, sometimes I replace it.

Is there a way to listen to FragmentTransactions completed, so I can keep track of every time a Fragment is added/shown/replaced?

I've tried fragmentManager.addOnBackStackChangedListener() but unsuccessfully.

I thought of doing something on the onResume() of all of them, but if the app goes to background and foreground I should not do anything to my UI. Only when I interact between them.

Any help/strategy is appreciated.

EDIT: The reason is that I have an overlay that will be shown in the current Fragment. If the user navigates to any other Fragment, I need this overlay to disappear.

Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • I honestly don't get why people just down vote a question without providing any feedback on regards to that. – Teo Inke May 28 '15 at 18:11

2 Answers2

1

If I got you correctly you do not need to listen to know this everytime - you just need to know that in regards of your application. If so, write FragmentHelper class (or add some methods to your Base fragment class if you have any) and wrap FragmentManager methods with it, incl. all the additional logic, tracking or everything else you require there.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I have an overlay that will be shown in the current Fragment. If the user navigates to any other Fragment, I need this overlay to disappear. That's why I think I need to be aware of all the Transactions. I'll take a look at what you suggested. Thanks – Teo Inke May 27 '15 at 22:03
  • Is a custom overlay that I have to add manually, hence also remove manually – Teo Inke May 27 '15 at 22:12
  • Unless I miss something about this overlay, this looks completely irrelevant. It is your activity, and it host the fragments – Marcin Orlowski May 27 '15 at 22:32
  • I have an Activity with about 5 Fragments interacting in many ways. I had an 'overlay' (a view) on one Fragment, and now my client requires me to show it on whatever is the current Fragment, but it must be shown only once. So I need o manage it manually and wanted a simple/high level manner to do it. I don't see why it's irrelevant – Teo Inke May 27 '15 at 23:03
  • Then rework your activity's layout. Put both your overlay and fragment container in i.e. `FrameLayout` or `RelativeLayout` (whereve overlay will be, well... over fragment container) and relieve fragments from a need of dealing with it. If you all done correctly, then your activity manages fragments anyway, so there will be no problem managing overlay – Marcin Orlowski May 28 '15 at 07:37
  • I have many callbacks from Fragments to the Activity to add/replace/show Fragments, and it's a lot of work to check everyone of them. Anyway, it became a useless discussion as there is no way to do what I intended to. I hope you at least understood what I'm doing. – Teo Inke May 28 '15 at 18:11
0

FragmentManager.BackStackEntry and pay attention to getName() by keeping reference to it in an array, and whenever onBackStackChanged() is called you will then know who has been removed or added.

Logic.edit

ArrayList<String> myNames; //will contain the tag id
// when onbackstackchanged is triggered
// get the one on top with getBackstackEntryAt(lastpos).getName()
//use that to check myNames.contains(String)
// i guess you can now put your logic in an if else statement
Elltz
  • 10,730
  • 4
  • 31
  • 59