Android Fragment中onCreateView和onViewCreated的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25119090/
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
Difference between onCreateView and onViewCreated in Fragment
提问by Smith
What's the essential difference between these two methods? When I create a TextView, should I use one over the other for performance?
这两种方法的本质区别是什么?当我创建一个 TextView 时,我应该使用一个而不是另一个来提高性能吗?
Edit: What's the difference from
编辑:有什么区别
onCreateView() {
root = some view
View v = new View(some context);
root.add(v);
return root;
}
onViewCreated() {
View v = new View(some context);
getView().add(v);
}
回答by Jawad Zeb
We face some crashes initializing view in onCreateView
.
我们在onCreateView
.
You should inflate your layout in
onCreateView
but shouldn't initialize other views usingfindViewById
inonCreateView
.
您应该在 in 中扩展您的布局,
onCreateView
但不应使用findViewById
in初始化其他视图onCreateView
。
Because sometimes view is not properly initialized. So always use findViewById
in onViewCreated
(when view is fully created) and it also passes the view as parameter.
因为有时视图没有正确初始化。所以总是使用findViewById
in onViewCreated
(当视图完全创建时)并且它还将视图作为参数传递。
onViewCreated
is a make sure that view is fully created.
onViewCreated
是确保完全创建视图。
onViewCreated android Documentation
onViewCreated android 文档
Called immediately after onCreateView
(android.view.LayoutInflater, android.view.ViewGroup
, android.os.Bundle
) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.
在onCreateView
( android.view.LayoutInflater, android.view.ViewGroup
, android.os.Bundle
) 返回之后立即调用,但在任何保存的状态恢复到视图之前调用。一旦他们知道他们的视图层次结构已经完全创建,这给了子类一个初始化自己的机会。然而此时片段的视图层次结构并未附加到其父级。
回答by u3l
onViewCreated
is called immediately after onCreateView
(the method you initialize and create all your objects, including your TextView
), so it's not a matter of performance.
onViewCreated
之后立即调用onCreateView
(您初始化和创建所有对象的方法,包括您的TextView
),因此这不是性能问题。
From the developer site:
从开发者网站:
onViewCreated(View view, Bundle savedInstanceState)
Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.
onViewCreated(View view, Bundle savedInstanceState)
在 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回后立即调用,但在任何保存的状态恢复到视图之前。一旦他们知道他们的视图层次结构已经完全创建,这给了子类一个初始化自己的机会。然而此时片段的视图层次结构并未附加到其父级。
Source: Fragment#onViewCreated
回答by orangemako
It's better to do any assignment of subviews to fields in onViewCreated
. This is because the framework does an automatic null check for you to ensure that your Fragment's view hierarchy has been created and inflated (if using an XML layout file) properly.
最好将子视图分配给onViewCreated
. 这是因为框架会自动为您检查空值,以确保您的 Fragment 的视图层次结构已正确创建和膨胀(如果使用 XML 布局文件)。
Code snippet from: FragmentManger.java
代码片段来自:FragmentManger.java
// This calls onCreateView()
f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), null, f.mSavedFragmentState);
// Null check avoids possible NPEs in onViewCreated
// It's also safe to call getView() during or after onViewCreated()
if (f.mView != null) {
f.mView.setSaveFromParentEnabled(false);
if (f.mHidden) f.mView.setVisibility(View.GONE);
f.onViewCreated(f.mView, f.mSavedFragmentState);
}
回答by Blackbelt
onCreateView
returns the inflated view. OnViewCreated
is called just after onCreateView
and get has parameter the inflated view. Its return type is void
onCreateView
返回膨胀的视图。在膨胀视图OnViewCreated
之后调用onCreateView
并获取参数。它的返回类型是void
回答by Phant?maxx
onCreateView()
is the Fragment equivalent of onCreate()
for Activities and runs duringthe View creation.onViewCreated()
runs afterthe View has been created.
onCreateView()
是 Fragment 等价onCreate()
于活动并在视图创建期间运行。创建视图后onViewCreated()
运行。
should I use one over the other for performance?
NO. There's no evidence of a performance boost.
should I use one over the other for performance?
否。没有性能提升的证据。
There is actually an onCreate()
method in Fragments too, but it's rarelyused (I do neveruse it, nor find a good use case for it).
onCreate()
Fragments 中实际上也有一个方法,但它很少使用(我从不使用它,也没有找到很好的用例)。
I always use onCreateView()
in Fragments as a replacement for onCreate()
.
And I'm happy with that.
我总是onCreateView()
在 Fragments 中使用作为onCreate()
.
我对此很满意。
回答by Peppe L-G
The docs for Fragment.onCreateView()
now says:
Fragment.onCreateView()
现在的文档说:
It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated(View, Bundle).
建议只在此方法中对布局进行膨胀,并将对返回的 View 进行操作的逻辑移动到 onViewCreated(View, Bundle)。
No need for us to understand why; we just need to do as the docs says, but it would be interesting to know why this recommendation exists. My best guess is separation of concern, but IMHO this makes it a little bit more complicated than it has to be.
我们不需要理解为什么;我们只需要按照文档说的去做,但是知道为什么存在这个建议会很有趣。我最好的猜测是关注点分离,但恕我直言,这使它比它必须的复杂一点。
回答by AmeyaB
The main reason I would use onViewCreated
is since it separates any initialization logic from the view hierarchy inflation/creation logic which should go in the onViewCreate
. All other performance characteristics look the same.
我会使用的主要原因onViewCreated
是因为它将任何初始化逻辑与应该在onViewCreate
. 所有其他性能特征看起来都一样。
回答by Shahriar enayaty
i think the main different between these is when you use kotlin.in onCreateView()every Time you want to access to view in your xml file you should use findViewByIdbut in onViewCreatedyou can simply access to your view just by calling the id of it.
我认为这些之间的主要区别在于,当您每次想要访问 xml 文件中的视图时都使用 kotlin.in onCreateView()时,您应该使用findViewById但在onViewCreated 中,您只需调用它的 id 即可轻松访问您的视图.
回答by Salu Khadka
onCreateView is used in fragment to create layout and inflate view. onViewCreated is used to reference the view created by above method. Lastly it is a good practice to define action listener in onActivityCreated.
onCreateView 在片段中用于创建布局和膨胀视图。onViewCreated 用于引用上述方法创建的视图。最后,在 onActivityCreated 中定义动作侦听器是一个很好的做法。