在片段差异与的onCreate(),onCreateView()和onActivityCreated()的用途 [英] Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

查看:161
本文介绍了在片段差异与的onCreate(),onCreateView()和onActivityCreated()的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间有什么的onCreate() onCreateView() onActivityCreated的差异()的片段,什么将他们各自用的呢?

What are the differences between onCreate(), onCreateView(), and onActivityCreated() in fragments and what would they each be used for?

推荐答案

的onCreate():

的onCreate()方法片段后称为活动 onAttachFragment() ,但在此之前,片段 onCreateView()
在这种方法中,你可以分配变量,得到意图演员,和别的不涉及View层次结构(即非图形initialisations )。这是因为这种方法可以被称为当活动的onCreate()还没有完成,所以试图访问View层次这里可能会导致系统崩溃。

The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView().
In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.

onCreateView():

的onCreate()被称为(在片段)的片段 onCreateView()被调用。您可以将您的查看变量和做任何图形initialisations 。您预计将返回查看这个方法,这是主要的用户界面视图,但如果你的片段做不使用任何布局或图形,你可以返回

After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View to this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null.

onActivityCreated():

在命名状态,这是活动后,名为的onCreate()已完成。这就是所谓的后 onCreateView(),主要用于最终initialisations(例如,修改UI元素)。

As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

归纳起来......
......他们都堪称片段而称为在不同的时间。
的onCreate()被称为第一,做任何非图形initialisations。接下来,你可以分配并声明任何查看变量要在 onCreateView使用()。然后,使用 onActivityCreated()做你想做的事,一旦一切都已经完成任何最终initialisations。

To sum up...
... they are all called in the Fragment but are called at different times.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView(). Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

如果您要查看的官方Android文档,它可以在这里找到:
- <一个href="http://developer.android.com/reference/android/app/Fragment.html#onCreate(android.os.Bundle)"><$c$c>onCreate()
- [ onCreateView()(<一href="http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater">http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup,android.os.Bundle))
- <一个href="http://developer.android.com/reference/android/app/Fragment.html#onActivityCreated(android.os.Bundle)"><$c$c>onActivityCreated()

If you want to view the official Android documentation, it can be found here:
- onCreate()
- [onCreateView()](http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle))
- onActivityCreated()

也有一些略有不同,但欠发达的问题/答案在这里堆栈溢出: - <一个href="http://stackoverflow.com/questions/27227906/where-is-the-diferent-between-oncreate-and-oncreateview"><$c$c>onCreate() VS onCreateView()
- <一个href="http://stackoverflow.com/questions/8041206/android-fragment-oncreateview-vs-onactivitycreated"><$c$c>onCreateView() VS onActivityCreated()

There are also some slightly different, but less developed questions/answers here on Stack Overflow: - onCreate() vs onCreateView()
- onCreateView() vs onActivityCreated()

这篇关于在片段差异与的onCreate(),onCreateView()和onActivityCreated()的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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