In Android, if you have the Application context you can register an Application.ActivityLifecycleCallbacks
instance that will be called everytime an Activity goes through one of its lifecycle callbacks.
How can I accomplish the same for fragments? I think there is no such interface for Fragments nor any clear place where I would add that.
Maybe customizing a FragmentHostCallback
creating a FragmentController
but how can I plug that for the whole application?
The use case is a library that needs to be notified everytime a Fragment calls its lifecycle callbacks and I don't want to create a BaseFragment. I want to be called only from Application's onCreate and that's it (if possible...).
EDIT:
I've created an issue in Android Open Source Project about this.
Since version 25.2.0 of Android support library, the class
FragmentManager.FragmentLifecycleCallbacks
is static and accessible to all.We can now use an instance of that class and register it in the
supportFragmentManager
of theActivity
.you can create a class like this, Singleton would be preferred
You will have to override lifecycle methods of Fragments, then you can initialize this class anywhere as you may please. Hope this helps
Well the activity dispatches its callbacks for the FragmentManager. Later the fragment manager dispatch the calls to the Fragment. There is no place to register the FragmentLifeCycleCallback in the FragmentManager. However at anytime after the fragment is initialized u can get the Fragment state from mState variable in the Fragment. you can only track the backstack in the fragment manager as below :
hope this will help.