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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:06:43  来源:igfitidea点击:

Difference between onCreateView and onViewCreated in Fragment

androidandroid-layoutandroid-fragments

提问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 onCreateViewbut shouldn't initialize other views using findViewByIdin onCreateView.

您应该在 in 中扩展您的布局,onCreateView但不应使用findViewByIdin初始化其他视图onCreateView

Because sometimes view is not properly initialized. So always use findViewByIdin onViewCreated(when view is fully created) and it also passes the view as parameter.

因为有时视图没有正确初始化。所以总是使用findViewByIdin onViewCreated(当视图完全创建时)并且它还将视图作为参数传递。

onViewCreatedis 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

onViewCreatedis 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

来源:片段#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

onCreateViewreturns the inflated view. OnViewCreatedis called just after onCreateViewand 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 onViewCreatedis 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 中定义动作侦听器是一个很好的做法。