Android:将活动的 onCreate() 代码放在片段中的什么位置?
我正在将我的所有活动转换为片段,以便我可以在 ViewPager 中使用它们.
I'm converting all my Activities to Fragments so that I can use them in a ViewPager.
我已经搜索过这个,但我找不到令人满意的答案,所以我在这里问它.
I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here.
在我的活动中,我在 onCreate()
方法中编写了一些代码.例如,我调用一些 findViewById()
来将一些 xml 按钮链接到我的 Activity.我还在 onCreate()
中使一些视图不可见,设置 OnClickListener()
,用文本填充 TextView
并删除通知,所有在 onCreate()
方法中.
In my Activities, I've written some code in the onCreate()
method. I for example call some findViewById()
s in order to link some xml-buttons to my Activity. I also make some views invisible in the onCreate()
, set an OnClickListener()
, fill a TextView
with text and remove a Notification, all in the onCreate()
method.
我的问题是:我应该把这段代码放在片段的哪里?在 onCreate()?创建视图()?onActivityCreated()?为什么?
My question is: Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?
非常感谢!
推荐答案
虽然 Pragnani 的答案很接近,但它没有什么教育价值.此外,他的第二个陈述还有一个更合适的选择.
Although Pragnani's answer is close, there's little educational value in it. Besides, there's a more appropriate option to his 2nd statement.
我应该将此代码放在片段中的什么位置?在 onCreate()?创建视图()?onActivityCreated()?为什么?
Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?
简短的回答是:onCreateView()
或 onActivityCreated()
都可以.在 onCreateView()
之前不会创建视图层次结构,因此这是片段生命周期中最早可以膨胀视图并附加点击侦听器等的时间点.因为 onActivityCreated()
将始终在 onCreateView()
之后运行/a>,这也是一个合适的位置.onCreate()
可能会被跳过,以支持系统暂时分离片段并重新附加它,例如当保留片段.
The short answer is: either onCreateView()
or onActivityCreated()
will do. The view hierarchy won't be created until onCreateView()
, so that's the earliest point in the fragment's life cycle that you could inflate the views and attach click listeners etc. Since onActivityCreated()
will always be run after onCreateView()
, that's a suitable location too. onCreate()
may be skipped in favour of the system temporarily detaching the fragment and reattaching it, e.g. when retaining fragments.
Pragnani 是正确的,他指出膨胀片段的视图与膨胀活动中的视图略有不同.更具体地说:片段没有定义 findViewById()
方法,因此您需要在其他对象上调用它.
Pragnani is correct by pointing out that inflating the views of a fragment is slightly different from inflating views in an activity. More specifically: a fragment does not define a findViewById()
method, so you'll need to call it on some other object.
您需要 getView().findViewById()
,而不是使用 getActivity().findViewById()
.这样做的原因是,如果您使用 Activity 进行视图查找,那么当多个具有相同视图 ID 的片段附加到它时,您会遇到麻烦.如果您在各种片段的布局中重用视图 ID,或者如果您显示两个显示不同数据的相同片段,就会出现这种情况.在这两种情况下,只会返回第一个匹配项,而您确实希望在片段的上下文中查找视图.这正是 getView()
返回片段的根视图(您在 onCreateView()
中返回的),从而适当地限制了查找的范围.
Rather than using getActivity().findViewById()
, you'll want getView().findViewById()
. The reason for this is that if you use the activity for the view lookups, then you'll get into trouble when multiple fragments with the same view IDs are attached to it. This will be the case if you reuse view ids in the layouts of your various fragments, or if you show two identical fragments that display different data. In both cases, only the first match would ever be returned, whereas you really want to the view to be looked up in the conext of the fragment. That's exactly what getView()
returns, the fragment's root view (that you returned in onCreateView()
), and thus limits the scope of the lookup appropriately.
相关文章