Difference between onCreateView and onViewCreated in Fragment

What is the significant difference between the two methods? When I create a TextView, should I use one over the other for performance?

Edit: What is 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); } 
+96
android android-layout android-fragments
Aug 4 '14 at 12:49 on
source share
8 answers

We come across some onCreateView initialization in onCreateView .

You must inflate your layout in onCreateView but you must not initialize other views using findViewById in onCreateView .

Because sometimes the view is not properly initialized. Therefore, always use findViewById in onViewCreated (when the view is fully created), and it also passes the view as a parameter.

onViewCreated - make sure the view is fully created.

onViewCreated Android documentation

Called immediately after onCreateView ( android.view.LayoutInflater, android.view.ViewGroup , android.os.Bundle ), but before restoring any saved state in the view. This gives subclasses the ability to initialize themselves as soon as they find out that their presentation hierarchy is fully created. However, at this stage, the fragment presentation hierarchy is not tied to its parent.

+58
Aug 02 '16 at 10:40
source share

onViewCreated is called immediately after onCreateView (the method that you initialize and create all your objects, including your TextView ), so this is not a performance issue.

From the developer's site:

onViewCreated (View view, Bundle saveInstanceState)

Called immediately after returning onCreateView (LayoutInflater, ViewGroup, Bundle), but before restoring any saved state in the view. This gives subclasses the ability to initialize themselves as soon as they find out that their presentation hierarchy is fully created. However, at this stage, the fragment presentation hierarchy is not tied to its parent.

Source: Fragment # onViewCreated

+35
Aug 04 '14 at 12:52 on
source share

It's best to do any subviews job in fields in onViewCreated . This is because the framework performs an automatic zero check to make sure that the presentation hierarchy of fragments has been created and overestimated (if an XML layout file is used) properly.

Code snippet from: FragmentManger.java

 // This calls onCreateView() f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), null, f.mSavedFragmentState); // Null check avoids possible NPEs in onViewCreated // It 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); } 
+25
Aug 6 '15 at 16:48
source share

onCreateView returns an inflated view. OnViewCreated is called immediately after onCreateView , and get has an inflated view parameter. Its return type is void

+12
Aug 04 '14 at 12:52 on
source share

onCreateView() is the equivalent of the onCreate() fragment for operations and is executed when the view is created.
onViewCreated() starts after the view has been created.

should I use one over the other for performance? NO . There is no evidence of increased productivity.

Framents also has an onCreate() method.

But it is rarely used (I never use it ), and I can not find a suitable option for it). I always use onCreateView() in Fragments as a replacement for onCreate() .
And I am pleased with that.

+4
Aug 04 '14 at 12:54 on
source share

onCreateView is used in the fragment to create the layout and bloat. onViewCreated is used to reference the view created by the above method. Finally, it is good practice to define an action listener in onActivityCreated.

+1
Jun 13 '17 at 7:54 on
source share

The main reason I will use onViewCreated is that it separates any initialization logic from the inflation / creation hierarchy logic of the view, which should go into onViewCreate . All other performance characteristics look the same.

+1
Aug 14 '17 at 23:47 on
source share

I think the main difference between the two is that when you use kotlin.in onCreateView () every time you want to access the view in your XML file, you should use findViewById, but in onViewCreated you can just access to your view, simply by calling its identifier,

+1
Apr 23 '18 at 10:45
source share



All Articles