When is onAttach called during the Fragment LifeCy

2019-01-12 18:22发布

问题:

This question already has an answer here:

  • Why is onAttach called before onCreate? 4 answers

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?

回答1:

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: http://baiduhix.blogspot.com.br/2015/08/android-how-to-do-findviewbyid-in.html

EDIT:

updated lifecycle of Android

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



回答2:

onAttach is called after Fragment is associated with its Activity.

http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.content.Context)