Exactly when is onAttach
called during the Fragment-Activity lifecycles?
I am aware of the Fragment Life-Cycle but exactly when?
Is it when the fragment object is initiated or when fragment manager is called?
onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
onAttach() will be called first, even before onCreate() , letting you know that your fragment has been attached to an activity. You are passed the Activity that will host your fragment. onViewCreated() will be called after onCreateView() .
Interview Answer. OnAttach() method is going to be called only once.
onAttach() : Called when a Fragment is first attached to a host Activity . Use this method to check if the Activity has implemented the required listener callback for the Fragment (if a listener interface was defined in the Fragment ). After this method, onCreate() is called.
In activity the first method is onCreate. onCreate of activity add the fragment and in this moment onAttach is called. like in the picture
Ref: https://github.com/xxv/android-lifecycle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With