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.
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 wrapFragmentManager
methods with it, incl. all the additional logic, tracking or everything else you require there.FragmentManager.BackStackEntry
and pay attention togetName()
by keeping reference to it in an array, and wheneveronBackStackChanged()
is called you will then know who has been removed or added.Logic.edit