Home » Android » android – Listener for fragment lifecycle

android – Listener for fragment lifecycle

Posted by: admin June 16, 2020 Leave a comment


If I have a fragment object, is there a way I could get notified of the lifecycle methods of that fragment ?

One possible way to do this is to implement this mechanism in my fragment and the fragment notifies the listeners, but this requires boilerplate code which I need to add in all the methods of the fragment. Is there a support for this at the Android platform level ?

How to&Answers:

You can register a FragmentLifecycleCallbacks instance using supportFragmentManager.registerFragmentLifecycleCallbacks(callback). The callback exposes all lifecycle events of a fragment, and is active until the fragment is destroyed (in which case the callback is automatically unregistered for you).