安卓:如果把活动的onCreate()code的片段? [英] Android: Where to put activity's onCreate() code in a fragment?

查看:171
本文介绍了安卓:如果把活动的onCreate()code的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我所有的活动,以片段,这样我就可以使用他们在一个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.

在我的活动,我已经写了一些code。在的onCreate()方法。我例如调用一些 findViewById() S在为一些XML-按钮链接到我的活动。我也提出一些意见无形之中的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.

我的问题是:我应该在哪里把这个code的片段?在OnCreate()? onCreateView()? 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.

我应该在哪里把这个code的片段?在OnCreate()?   onCreateView()? onActivityCreated()?为什么?

Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?

简短的回答是:要么 onCreateView() onActivityCreated()就行了。该视图层次结构将不会被创建直到 onCreateView(),所以这是在片段的生命周期中最早的点,你可以膨胀的意见,并附上点击监听器等。<一个HREF =htt​​p://developer.android.com/guide/components/fragments.html#Lifecycle>因为 onActivityCreated()后,总是会执行 onCreateView() ,这是一个合适的位置了。 的onCreate()可支持系统暂时分离的片段,再重新连接,如对被跳过当<一href="http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29">retaining片段的。

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.

而不是使用 getActivity()。findViewById(),你会想 getView()。findViewById() 。这样做的原因是,如果你使用活动视图查找,那么你就会陷入困境时,用相同的观点ID的多个片段连接到它。这会是这样,如果你重用你的各种片段的布局视图ID,或者如果你显示,显示不同的数据两个相同的片段。在这两种情况下,只有第一场比赛将永远被退回,而你真的想在视图中找出该片段的conext。这正是<一href="http://developer.android.com/reference/android/app/Fragment.html#getView%28%29"><$c$c>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.

这篇关于安卓:如果把活动的onCreate()code的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆