Java 在 Fragment LifeCycle 期间什么时候调用 onAttach?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36339811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is onAttach called during the Fragment LifeCycle?
提问by Ackman
Exactly when is onAttach
called during the Fragment-Activity lifecycles?
onAttach
在 Fragment-Activity 生命周期中究竟何时被调用?
I am aware of the Fragment Life-Cycle but exactly when?
我知道 Fragment Life-Cycle 但具体是什么时候?
Is it when the fragment object is initiated or when fragment manager is called?
是在启动片段对象时还是在调用片段管理器时?
采纳答案by Lucas Ferraz
In activity the first method is onCreate.
onCreate of activity add the fragment and in this moment onAttach is called. like in the picture
在活动中,第一个方法是 onCreate。活动的 onCreate 添加片段,此时 onAttach 被调用。就像图片中一样
Ref: http://baiduhix.blogspot.com.br/2015/08/android-how-to-do-findviewbyid-in.html
参考:http: //baiduhix.blogspot.com.br/2015/08/android-how-to-do-findviewbyid-in.html
EDIT:
编辑:
updated lifecycle of Android
更新的 Android 生命周期
回答by Wilder Pereira
onAttach is called after Fragment is associated with its Activity.
在 Fragment 与其 Activity 关联后调用 onAttach。
http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.content.Context)
http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.content.Context)