在片段差异与的onCreate(),onCreateView()和onActivityCreated()的用途片段、差异、用途、onCreate

2023-09-03 22:50:51 作者:痞浪女ぃ

之间有什么的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文档,它可以在这里找到: - onCreate() - [ onCreateView()(http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup,android.os.Bundle)) - 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()

也有一些略有不同,但欠发达的问题/答案在这里堆栈溢出: - onCreate() VS onCreateView() - onCreateView() VS onActivityCreated()

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