Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is onAttach called during the Fragment LifeCycle? [duplicate]

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?

like image 378
Ackman Avatar asked Mar 31 '16 17:03

Ackman


People also ask

What is onAttach in fragment?

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.

Is onAttach called before onViewCreated?

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() .

Which method is called only once in a fragment lifecycle?

Interview Answer. OnAttach() method is going to be called only once.

How do you use onAttach?

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.


1 Answers

In activity the first method is onCreate. onCreate of activity add the fragment and in this moment onAttach is called. like in the picture

enter image description here

Ref: https://github.com/xxv/android-lifecycle

like image 160
Lucas Ferraz Avatar answered Sep 24 '22 06:09

Lucas Ferraz