问题描述
关于 Android 注释,
About Android Annotations,
@AfterViews 相当于 onCreate 还是 onPostCreate?
@AfterViews is equivalent to onCreate or onPostCreate?
或者每个注释的等效注释是什么?
or what is the annotation equivalent for each one?
我可以删除onCreate"和onPostCreate"方法,并将我的全部代码与@AfterViews 放在同一个方法中吗?
can i remove "onCreate" and "onPostCreate" methods and put my whole code of both in the same method with @AfterViews?
推荐答案
在带注释的 Activity
中,@AfterViews
带注释的方法在 onCreate() 中被调用代码>.在带注释的
Fragment
中,@AfterViews
带注释的方法在 onViewCreated()
中被调用.如果您不想在创建视图之前进行初始化,您可以安全地删除您的 onCreate()
方法并在 @AfterView
的注释方法中执行所有操作.然而,AfterViews
的主要原因是调用者有机会初始化注入的视图,这些视图在 onCreate()
中尚不可用.
In annotated Activity
s, the @AfterViews
annotated methods are called in onCreate()
. In annotated Fragment
s, the @AfterViews
annotated methods are called in onViewCreated()
. If you do not want to do initialization before views are created, you can safely remove your onCreate()
method and do everything in an @AfterView
s annotated method.However the main reason of AfterViews
is getting a chance to the caller to initialise injected views, which are not yet available in onCreate()
.
这篇关于@AfterViews 相当于 onCreate 还是 onPostCreate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!