简体   繁体   English

Android:在片段中放置活动的onCreate()代码的位置?

[英]Android: Where to put activity's onCreate() code in a fragment?

I'm converting all my Activities to Fragments so that I can use them in a ViewPager. 我将我的所有活动转换为碎片,以便我可以在ViewPager中使用它们。

I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here. 我已经搜索了这个,但我找不到令人满意的答案,所以这就是我在这里问的原因。

In my Activities, I've written some code in the onCreate() method. 在我的活动中,我在onCreate()方法中编写了一些代码。 I for example call some findViewById() s in order to link some xml-buttons to my Activity. 我例如调用一些findViewById()来将一些xml按钮链接到我的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()创建了一些不可见的视图,设置了一个OnClickListener() ,用文本填充TextView并删除了一个Notification,这些都在onCreate()方法中。

My question is: Where should I put this code in the fragment? 我的问题是:我应该在片段中放置这些代码? In the onCreate()? 在onCreate()? onCreateView()? onCreateView()? onActivityCreated()? onActivityCreated()? and why? 为什么?

Many thanks in advance! 提前谢谢了!

Although Pragnani's answer is close, there's little educational value in it. 虽然Pragnani的答案很接近,但它的教育价值却很小。 Besides, there's a more appropriate option to his 2nd statement. 此外,他的第二个陈述更合适。

Where should I put this code in the fragment? 我应该把这段代码放在片段里? In the onCreate()? 在onCreate()? onCreateView()? onCreateView()? onActivityCreated()? onActivityCreated()? and why? 为什么?

The short answer is: either onCreateView() or onActivityCreated() will do. 简短的回答是: onCreateView()onActivityCreated()都可以。 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. onCreateView()之前不会创建视图层次结构,因此这是片段生命周期中最早的一点,你可以膨胀视图并附加点击监听器等等。 因为onActivityCreated()将始终在onCreateView()之后运行 ,这是一个合适的位置。 onCreate() may be skipped in favour of the system temporarily detaching the fragment and reattaching it, eg when retaining fragments . 可以跳过onCreate()以支持系统临时分离片段并重新附加片段,例如在保留片段时

Pragnani is correct by pointing out that inflating the views of a fragment is slightly different from inflating views in an activity. Pragnani是正确的,指出膨胀片段的视图与活动中的膨胀视图略有不同。 More specifically: a fragment does not define a findViewById() method, so you'll need to call it on some other object. 更具体地说:片段没有定义findViewById()方法,因此您需要在其他对象上调用它。

Rather than using getActivity().findViewById() , you'll want getView().findViewById() . 而不是使用getActivity().findViewById() ,你需要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. 这样做的原因是,如果您将活动用于视图查找,那么当具有相同视图ID的多个片段附加到该视图时,您将遇到麻烦。 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. 如果在各种片段的布局中重复使用视图ID,或者显示两个显示不同数据的相同片段,则会出现这种情况。 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. 在这两种情况下,只会返回第一个匹配,而您真的希望在片段的conext中查找视图。 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. 这正是getView()返回的内容,片段的根视图(您在onCreateView()返回的),因此适当地限制了查找的范围。

1. Left the onCreate empty and just call super.onCreate() 1. 将onCreate留空,只需调用super.onCreate()

2. Instead of findViewById() use getActivity().findViewById() always use getActivity() where you need context of the view. 2. 使用getActivity()而不是findViewById()。findViewById()始终使用getActivity(),您需要视图的上下文。

 Do all other operations in onCreateview()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM